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: "))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.