0

Here is a practical example ...

def get_int():
    """this function will loop until an integer is entered"""
    while True:
        try:
            # return breaks out of the endless while loop
            return int(raw_input("Enter an integer: "))
        except ValueError:
            print("Try again, value entered was not an integer.")

myinteger = get_int()
print("You entered %d" % myinteger)

For Python3 use:
return int(input("Enter an integer: "))

Edited by vegaseat: also accepts -2

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.