[Tutor] Suggestions for while loop

David david at abbottdavid.com
Mon May 11 22:42:50 CEST 2009


Dave Angel wrote:

> 
> def getTotalNumber(prompt):
>    while True:
>        try:
>            numstr = raw_input(prompt)
>            n = int(numstr)
>            if  0 < n < 50:            #some validation
>                 return n
>            print "Number is not reasonable, try again"
>       except ValueError:
>            print "You must enter a number!"
> 
Richard "Roadie Rich" Lovely wrote:

> total_names = -1 # set an invalid initial value
> while not 0 < total_names < 50: # loop while total_names has an invalid value
>     try:
>         # If this excepts, total_names is not changed.
>         total_names = int(raw_input('Enter Total Number of Names: '))
>         # if total_names now has a valid value, the loop exits.
>     except ValueError:
>         print 'You must enter a number!' # loop continues.

Thanks Dave and Richard,

And to run it is this correct

total_names = getTotalNumber('Enter Total Number of Names: ')


-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list