[Tutor] Is it pythonesque

Robert Berman bermanrl at cfl.rr.com
Sun Jan 24 17:13:37 CET 2010

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'


            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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100124/e902eac3/attachment.htm>

More information about the Tutor mailing list