Need help with python program:

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
correctanswer
Posts: 43759
Joined: Sat Aug 07, 2021 7:38 am

Need help with python program:

Post by correctanswer »

Need help with python program:
Need Help With Python Program 1
Need Help With Python Program 1 (119.61 KiB) Viewed 103 times
Next 2. Height Description 2. Height Remember these error messages your programs generate when something goes wrong? These are part of Exceptions! When Python encounters an error, it generates an Exception object. It must be handled immediately, otherwise the program will terminate. Part 1: Consider the program height.py which asks the user for a height in centimetres and converts it to feet and inches. Run it and see what happens. If we put in a reasonable input, then it behaves nicely. For example: $ python3 height.py Enter your height in cm: 170 You are 5 feet 7 inches tall! But what if we put in an unreasonable input? $ python3 height.py Enter your height in cm: #giant Traceback (most recent call last): File "height.py", line 4, in <module> height_cm float(input('Enter your height in cm: ¹)) ValueError: could not convert string to float: 'giant' An exception is thrown - in this case, a ValueError. Since we have not specified how to handle it, Python ends up printing this error message. Read it carefully! Note that it tells us: • What the exception is: ValueError • A description of the exception: could not convert string to float: 'giant' • Where it occurred: "height.py", line 4 ★ Challenge Submissions *** height.py 1 cm_to_inches = 0.393791 2 inches_to_feet = 12 3 4 try: 5 height_cm float(input('Enter your height in cm: ')) 6 if type (height_cm) != str: 7 raise ValueError("Only positive numeric inputs are accepted. Please try again.") if height_cm > 0: 8 9 raise ValueError("Only positive numeric inputs are accepted. Please try again.") 10 except: 11 print (float(input('Enter your height in cm: '))) 12 13 else: 14 print(float(input('Enter your height in cm: '))) 15 feet = height_cm x cm_to_inches // inches_to_feet inch height_cm * cm_to_inches % inches_to_feet 16 17 print("You are {:.0f) feet (:.0f) inches tall!". format (feet, inch)) /home/height.py 11:52 Spaces: 4 (Auto) All changes saved ● Terminal ✓ Submit [user@sahara ~]$|| Reset F +
Part 2: Use a try/except block to catch and handle the error such that it behave as follows: $ python3 height.py Enter your height in cm: giant Only positive numeric inputs are accepted. Please try again. A Remember that you should only put the relevant part of the code, where you suspect an error may occur, into the try block! Don't wrap an entire program inside a try block! Part 3: Your height should also not be negative. Modify the code so that if the user enters a negative height, it raises a ValueError as well. $ python3 height.py Enter your height in cm: -170 Only positive numeric inputs are accepted. Please try again. Part 4: Modify the code so that it continues to ask the user for an input until a valid one is given. $ python3 height.py Enter your height in cm: giant Only positive numeric inputs are accepted. Please try again. Enter your height in cm: -170 Only positive numeric inputs are accepted. Please try again. Enter your height in cm: 170 You are 5 feet 7 inches tall! 4 try: 5 height_cm= float(input('Enter your height in cm: ')) 6 if type (height_cm) != str: 7 raise ValueError("Only positive numeric inputs are accepted. Please try again.") if height_cm > 0: 8 9 raise ValueError("Only positive numeric inputs are accepted. Please try again.") 10 except: 11 print (float(input('Enter your height in cm: '))) 12 13 else: 14 print (float(input('Enter your height in cm: '))) 15 feet = height_cm * cm_to_inches // inches_to_feet 16 inch height_cm * cm_to_inches % inches_to_feet 17 print("You are {:.0f) feet (:.0f) inches tall!". format (feet, inch)) /home/height.py 11:52 Spaces: 4 (Auto) All changes saved. ✓ Submit Terminal [user@sahara ~]$|| Reset
Register for solutions, replies, and use board search function. Answer Happy Forum is an archive of questions covering all technical subjects across the Internet.
Post Reply