[Tutor] Is it pythonesque

शंतनू shantanoo at gmail.com
Sun Jan 24 17:30:11 CET 2010


If the input has more than one ',', e.g. '1,2,3'. You may like to use 'split' function, and avoid find.

On 24-Jan-2010, at 9:43 PM, Robert Berman wrote:

> Good morning,
>  
> Given the following code snippets:
>  
> def getuserinput():
>     while True:
>         s1 = raw_input('Enter fraction as N,D or 0,0 to exit>>')
>         delim = s1.find(',')
>         if delim < 0:
>             print 'invalid user input'
>         else:
>             n = int(s1[0:delim])
>             d = int(s1[delim+1::])
>             return n,d
>  
> def main():
>     while True:
>         n,d = getuserinput()
>         if n == 0 or d == 0: return 0
>  
> n and d are always returned provided the input is given as n,d. n/d will print an error message and the loop will reiterate until the user format is correct. Please note there is no true ending return for getuserinput() as it is hung off an if statement and if by some  chance it breaks, it should return None which will abort the program. While I suspect this style is devious, is it dangerous or ‘wrong’ to use. Comments and other renditions are most welcome.
>  
> Thank you,
>  
> Robert Berman
>  
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list