Question: PYTHON: Modify the following code to use exception handling, such that if the user enters in a character string, such as hello, the exception handling

PYTHON: Modify the following code to use exception handling, such that if the user enters in a character string, such as "hello", the exception handling code will gracefully handle the exception, tell the user what he/she did wrong, and continue to ask the user to enter the correct number.

Note: Do not use the isdigit() method with your answer. You should complete this assignment using exception handling.

USER_INPUT_MIN = 1 USER_INPUT_MAX = 20

def main() : answer = 0 while (answer < USER_INPUT_MIN or answer > USER_INPUT_MAX) : answer = int(input("Enter number: ")) if (answer < USER_INPUT_MIN or answer > USER_INPUT_MAX) : print("Please enter a number between %d and %d" % (USER_INPUT_MIN, USER_INPUT_MAX))

print("Got the user's answer: ", answer) main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!