Modify your program to include try/except for the keyword inputs. Included a fin
ID: 3738161 • Letter: M
Question
Modify your program to include try/except for the keyword inputs. Included a finally with the try/except to indicate that the program has ended.I got this far but I’m not sire where to go from here 4 @author: Sam 7 #create variables 8 peopleweight [] 9 num Weights = 4 10 11#ask for user weights 12 for i in range(num_Weights): singleweight = float(input( "Enter weight?: " % (1+1))) 14 peopleweight.append (singleweight) 15 16 print("Wetghts:peopleweight) 17 18#Output average 19 avgweight sum(peopleweight)/len(peopleweight) 20 print( "Average weight: %.2f"X avgWeight) 21 22 #Output max weight 23 print( "Max weight: %.2f" Xmax(people!eight)) 24 25 #Ask user for list element 26 index - int(input( "Enter an index number: (1-4)")) 27 chosenkleight peopleweight[index -1] 28 print( "Weight in pounds: %.zf" % chosenlleight) 29 prinda"Weight in kiklograms: %Af" %(chosenkleight/2.2)! 30 31 #sort the list 32 peopleweight.sort) 33 print("Sorted List: ".peopleweight) 34 35 peopleweight.sort(reverse True) 36 print ("Reverse Sorted List: ",peopleweight) 37 Console O Type here to search
Explanation / Answer
The syntax for try/except is very simple. You put try block and then the code which you think will face inside this block. After this block add expect block which deals with the exceptions occurred in the code of try block. This means expect block will execute only of something throws an exception in try block The finally block will get executed irrespective of errors or clean run.
Syntax:
try:
#some code
expect:
#somecode
finallly:
#some code
For the program that you have mentioned. Add "try:" at line 10 and indent every line after line 10 under "try:". Then put "expect:" in the end (after line 37) and add some print statement showing the error.
After this add "finally:" and add a print statement to it "Program Ends.".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.