Beginner trying to understand functions.

Arnaud Delobelle arnodel at googlemail.com
Tue Dec 9 15:31:15 EST 2008


Ivan Illarionov <ivan.illarionov at gmail.com> writes:

> 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()

That's not necessarily good advice as Python does not optimize
tail-calls.

-- 
Arnaud



More information about the Python-list mailing list