# File: except2.py # Program to read in a positive number # and complain when the input isn't that # # Matt Bishop, MHI 289I, Winter 2018 # # loop to show exception handling # while True: # read in a positive integer # handle any exceptions (relatively) intelligently try: n = int(raw_input("type a number: ")) # exception: bad value (string, invalid number) except ValueError: print "bad value -- try again!" # give it another chance # exception: end of file except EOFError: print "end of file -- bye!" # quit break # some other exception except: print "Unknown exception -- ignoring!" # ignore the problem # no exception -- print the number else: if n <= 0: print "You typed a non-positive number" else: print "Read", n print "\n-----------------------\n"