# File: badger4.py # # Keep asking the user to type in a number # until the user types in a valid number. # # This version doesn't quit even if the # user types in ctrl-C. # done = False while not done: try: # get input string and convert to integer # userInput = input("Enter a number: ") n = int(userInput) except ValueError: # exception triggered for input not a valid # integer # print("That's not an integer! Try again.") except EOFError: # exception triggered if user types ctrl-D # print("Please type something! Try again.") except BaseException: # unknown error... keep going, anyway # try typing ctrl-Z (on Unix) # print("Unknown error encountered!") else: # no exceptions found. Can quit now. # print("Thank you!") done = True print("n is ", n)