Question

I'm just trying Python, and really like it! But I get stucked with try/except.

I have a code that checks raw_input for being integer, but i'd like to make it function, and it doesn't want to be it :)

here the code, I have this:

number_of_iterations = raw_input("What is your favorite number?")
try:
    int(number_of_iterations)
    is_number = True
except:
    is_number = False

while not is_number:
    print "Please put a number!"
    number_of_iterations = raw_input("What is your favorite number?")
    try:
        int(number_of_iterations)
        is_number = True
    except:
        is_number = False

I don't want to repeat myself here& So I think smth about to make function:

def check_input(input_number):
    try:
        int(input_number)
        return True
    except:
        return False

But it make an error if input a string, saying that int can't be used for strings. Looks like it does not see 'try' keyword. Can smone explain why it happens and how to prevent it in future?

Was it helpful?

Solution

Try this, it avoids repeating yourself without needing a def

while True:
    try:
        number_of_iterations = int(raw_input("What is your favorite integer?"))
        break
    except ValueError:
        print "Please put an integer!"

EDIT: Per the suggestions of the commenters, I have added break to the try portion of the block to eliminate the else (the original remains as a reference below). Also, I changed "number" to "integer" because "3.14" would be invalid in the above code.

This was my original suggestion. The above is fewer lines (some may call this cleaner), but I prefer the below because to me the intent is clearer.

while True:
    try:
        number_of_iterations = int(raw_input("What is your favorite integer?"))
    except ValueError:
        print "Please put an integer!"
    else:
        break

OTHER TIPS

If you want it as a function, decide two things:

  • What is it going to need to work, and
  • What it's going to need to spit back out.

Your use case is that you need some sort of int back, and if it bombs out for whatever reason, it's defined as not a number.

Let's create a tuple as our return value, so we can return a number of some kind and a boolean for "if it is a number".

def check_input(number):
    try:
        return (int(number), True)
    except ValueError:
        return (-999999, False) # Care more about boolean here

We can then use that return value in our while loop like so. Note that I explicitly set the loop condition to False, so we enter in at least once.

is_number = False
num = -999999 # Define out of scope of loop so it can be used

while not is_number:
    print "Please put a number!"
    num, is_number = check_input(raw_input("What is your favorite number?"))

The line num, is_number is a result of tuple packing. Since we're returning a tuple from our method, we can set two distinct variables to the results of that tuple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top