# Test module for the user input function # Lots of error checking # # Matt Bishop, MHI 289I, Fall 2023 # # function to read and vet integer that user input # returns: n, the number of tosses # NOTE: n must be positive integer; return -1 to quit def getinput(): # loop until we get good input while True: try: # get the input and check the type here n = int(input("number of tosses (EOF to quit): ")) except EOFError: # user wants to quit, so help n = -1 break except: # user didn't enter a number print("You have to enter a positive n or EOF") continue # got an integer # now check the value we read/were given if n > 0: # it's good! fall out break # this is bad input, so say so print("You have to enter a positive integer or EOF") # it's positive to continue, -1 to quit return n # main routine to enable us to test getinput() def main(): # get the input n = getinput() # loop until quit while n >= 0: # say what you got so user can check print("User entered", n) # get another input n = getinput() # all done! print("Quitting!") # # we're off! # main()