Question: Python 3 The goal is to add to program code below to check that the user input for number of questions and number of digits

Python 3

The goal is to add to program code below to check that the user input for number of questions and number of digits is, in each case, not only an integer (using code similar to the following if you want) and is also reasonable. By this I mean the user asking for 10,000,000 digits in the numbers might not be reasonable. Or asking for -1 questions. You can start thinking about this.while True: try: age = int(input("Please enter your age: ")) except ValueError: print("Sorry, I didn't understand that.") #better try again... Return to the start of the loop continue else: #age was successfully parsed! #we're ready to exit the loop. break if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.")

The try ... except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.

The program code is listed below. How would I correectly apply this new code to my program?

_______________________________________________________________-

import random

finished = False # condition that continues program

while not finished: # add or subtract option action = input('Do you wish to answer an Addtion (A) or Substraction (S): ') action = action.lower() # number of digits in math problem message = "Enter the number of digits to be added *1, 2, or 3 digits*: " #input from user d=int(input(message)) #all code in d==1 thru d==3 selects number of digits if d==1: firstnum = random.randrange(1,11) # returns int from 1 to 10 secondnum = random.randrange(1, 11) if d==2: firstnum = random.randrange(1,101) # returns int from 1 to 101 secondnum = random.randrange(1, 101) if d==3: firstnum = random.randrange(1,1001) # returns int from 1 to 1001 secondnum = random.randrange(1,1001) if action == "a": # addition problem compsum = firstnum + secondnum print("What is the sum of", firstnum, "+", secondnum, "?") elif action == "s": # subtraction problem compsum = firstnum - secondnum print("What is the difference of", firstnum, "-", secondnum, "?") else: # enter correct input for an addition or subtraction question print("Plese enter A for Addition or S for Subtraction") # users input result = int(input("Your answer is: ")) if result == compsum: # correct input from user print("You are correct!!!") else: # incorrect input from user print("Sorry, you are incorrect") # code to allow user to answer more questions choice = input('Do you want to answer another math question?:(Y for yes / N for no) ') if choice.lower() == "n": #ends program finished = True

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!