Beginner trying to understand functions.

Ivan Illarionov ivan.illarionov at gmail.com
Tue Dec 9 05:43:38 EST 2008


On Dec 8, 9:02 pm, simonh <simonharrison... at googlemail.com> wrote:
> Thanks for the many replies. Thanks especially to Pierre. This works
> perfectly:

<snip>

> def getAge():
>     while True:
>         try:
>             age = int(input('Please enter your age: '))
>             return age
>
>         except ValueError:
>             print('That was not a valid number. Please try again.')

You could also drop the while loop and the 'age' variable:

def getAge():
    try:
        return int(input('Please enter your age: '))
    except ValueError:
        print('That was not a valid number. Please try again.')
        return getAge()



More information about the Python-list mailing list