USING PYTHON 3: A) Keep asking the user to enter a number until they enter -999
ID: 3594527 • Letter: U
Question
USING PYTHON 3:
A)
Keep asking the user to enter a number until they enter -999 and the program should print the number and that number cubed.
Finally, print the sum of the cubes of all of the numbers.
Example run:
Enter a number -999 to stop: 2
2 8
Enter a number -999 to stop: 4
4 64
Enter a number -999 to stop: -999
Sum of cubes: 72
B)
Write PYTHON statements that print a message about a person’s age classification, which is based on their age (assume it’s stored in a variable called yourAge) . Use the following chart to see what to print:
Years Message
0-4 years “infant/toddler”
5-12 years “child”
13-19 years “teen”
older than 19 yrs “adult”
C)
Suppose myDict maps courseIDs to courseNames.
An example of this dictionary could be:
{“CSIS150” : “Intro Prog”, “MATH234” : “Statistics”, etc. }
1. Delete the entry with key “CSIS335” from the dictionary.
2. Ask the user for a courseID and a courseName and include that information int the dictionary.
3. Replace the courseName for “MATH234”. It should now be “Intro to Stats”
4. Print only the course IDS in alphabetical order.
5. Print only the course names.
D)
A dictionary maps ids to a list containing name and salary. An example is shown:
myDict = {111:[ ”Joe Bly”: 30000], 222:[”Jan Jones”: 200000], 333:[ ”Jeff Smith”:40000] }
NOTE: do NOT assume there will only be 3 entries in the dictionary!
a. print ONLY the names
b. replace Jan Jones’ salary – make it 400000
c. Ask the user to enter a name and delete the entry for that person from the dictionary.
d. Print the average of the salaries.
E)
Print the number of characters in a sentence entered by the user
What is the output? tmp = “Programming is fun”; print(tmp[4:8]
Given a string tmp, print the location of the last period.
Example: if tmp is “Mr. and Mrs. Brown”, the output should be 11.
Print “yes” if empName (a string) begins with “Mr.”
Print “yes” if myID (a string) has only letters and digits.
Print “yes” if sentence (a string) contains the word “CSIS153”
Explanation / Answer
A)
sumCubed = 0
while True:
number = int(input("Enter a number -999 to stop:: "))
if number == -999:
break
cube = number**3
sumCubed += cube
print(number, cube)
print("Sum of cubes:", sumCubed)
# copy pastable codE: https://paste.ee/p/3zm86
B)
if yourAge >= 0 && yourAge <= 4:
print("infant/toddler")
elif yourAge >= 5 && yourAge <= 12:
print("child")
elif yourAge >= 13 && yourAge <= 19:
print("teen")
elif yourAge > 19:
print("adult")
# copy pastable code link: https://paste.ee/p/2TOBr
C)
myDict = {"CSIS150" : "Intro Prog", "MATH234" : "Statistics", "CSIS335": "Dummy" }
del myDict["CSIS335"]
courseID = input("Enter a courseID: ")
courseName = input("Enter course name: ")
myDict[courseID] = courseName
myDict["MATH234"] = "Intro to Stats"
courseIDs = list(myDict.keys())
courseIDs.sort()
for courseID in courseIDs:
print(courseID)
for courseId in myDict:
print(myDict[courseId])
# copy pastable code link: https://paste.ee/p/ua27S
D)
myDict = {111:[ "Joe Bly", 30000], 222:["Jan Jones", 200000], 333:[ "Jeff Smith",40000] }
for id in myDict:
print(myDict[id][0])
for id in myDict:
if myDict[id][0] == "Jan Jones":
myDict[id][1] = 400000
break
name = input("Enter a name: ")
for id in myDict:
if myDict[id][0] == name:
del myDict[id]
break
salaries = 0
count = 0
for id in myDict:
salaries += myDict[id][1]
count += 1
average = salaries*1.0/count
print("Average: ", average)
# copy pastable code link: https://paste.ee/p/QVSSP
E)
sentence = input("Enter a sentence: ")
print(len(sentence))
tmp = "Programming is fun";
print(tmp[4:8])
tmp = "Mr. and Mrs. Brown"
print(tmp.rfind("."))
empName = "Mr. Brown"
if empName.startswith("Mr. "):
print("yes")
myID = '123abc'
if myID.isalnum():
print("yes")
sentence = "A sentence with CSIS153 in between"
if "CSIS153" in sentence:
print("yes")
# copy pastable code link: https://paste.ee/p/cDDRx
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.