python try/exception help -
i'm trying support repetition user inputs filename, inputs 2 integers. if exception thrown, want user prompted input again.
my problem if valid file entered invalid integer entered ask file again instead of integer. how can fix code ask integer again.
here have:
while true: try: f = raw_input("enter name of file: ") infile = open(f) # more code except ioerror: print ("the file not exist. try again.") else: try: integer = int(raw_input("enter integer: ")) integer2 = int(raw_input("enter integer: ")) # more code except (typeerror, valueerror): print ("not integer. try again.")
try use multiple while
loops:
while true: filename = raw_input("enter name of file: ") try: # more code here detect or open file break except exception: # can ioerror or else print ("error msg") while true: try: integer = int(raw_input("enter integer: ")) integer2 = int(raw_input("enter integer: ")) break except (typeerror, valueerror): print ("error msg") # more code
Comments
Post a Comment