Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books