# This is the user input function # Test file # ECS 10, May 11, 2009 # 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 = input("Enter the number of tosses: ") n += 0; except EOFError: # user wants to quit, so help her n = -1 break # oops! let error check below do the work # so set v to something illegal (not 0-3) except (SyntaxError, NameError, TypeError): n = -1 # 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 number" # it's non-negative 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()