# This is the user input function # Test file # # Matt Bishop, ECS 10, Fall 2012 # # function to read and vet user selection # returns: n, the number of tosses # NOTE: n must be positive; 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 her n = -1 break except ValueError: # user didn't enter a number print("You have to enter a positive n or EOF") continue # now check the value we read/were given if n > 0: break # this is bad, so say so print("You have to enter a positive n or EOF") # it's positve 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! say what user typed print("Quitting: n =", n) main()