michael
michael

Reputation: 61

python try/exception help

I'm trying to support repetition where the user inputs a filename, then inputs two integers. So if an exception is thrown, I want the user to be prompted with input again.

My problem is if a valid file is entered but an invalid integer is entered it will ask for the file again instead of just the integer. How can I fix the code so it will ask for the integer again.

Here is what I have:

while True:
    try:
        f = raw_input("Enter name of file: ")
        inFile = open(f)
        # more code
    except IOError:
        print ("The file does not exist. Try Again.")        
    else:    
        try:
            integer = int(raw_input("Enter an integer: "))
            integer2 = int(raw_input("Enter an integer: "))
            # more code
        except (TypeError, ValueError):
            print ("Not an integer. Try Again.")

Upvotes: 2

Views: 8212

Answers (4)

Yup.
Yup.

Reputation: 1893

To expand on an answer above regarding using multiple while loops and the related question about handling additional data verification without having to loop over already verified data, the following looks crufty but it gives you the granular inspection you're looking to achieve:

while True:
    fileName = raw_input("Enter name of file: ")
    try:
        # more code here to detect or open the file
        break
    except Exception:
        print ("The file does not exist. Try Again.")

while True:
    try:
        integer = int(raw_input("Enter an integer: "))
        break
    except (TypeError, ValueError):
        print ("Not an integer. Try Again.")

while True:
    try:
        integer2 = int(raw_input("Enter an integer: "))
        break
    except(TypeError, ValueError):
        print ("Not an integer. Try Again.")

Upvotes: 0

Sepp
Sepp

Reputation: 1

>>> def ask(question, func, exceptions):
...     while True:
...         try:
...             return func(raw_input(question))
...         except exceptions, error:
...             pass
... 
>>> x = ask("integer?", int, ValueError)
integer?o
integer?l
integer?42
>>> x
42

Upvotes: 0

Wang Dingwei
Wang Dingwei

Reputation: 4849

Try use multiple while loops:

while True:
    fileName = raw_input("Enter name of file: ")
    try:
        # more code here to detect or open the file
        break
    except Exception:  # can be IOError or something else
        print ("error msg")

while True:
    try:
        integer = int(raw_input("Enter an integer: "))
        integer2 = int(raw_input("Enter an integer: "))
        break
    except (TypeError, ValueError):
        print ("error msg")

# more code

Upvotes: 9

Jakob Bowyer
Jakob Bowyer

Reputation: 34698

while True:
    try:
        a = int(raw_input('a: '))
        b = int(raw_input('b: '))
        break
    except ValueError:
        print "Numbers idiot"
#more code here

This code will jump to the except block on a or b and carry on with more code if the numbers work.

Upvotes: 0

Related Questions