On Sun, 24 Jan 2010 11:13:37 -0500 "Robert Berman" <berma...@cfl.rr.com> 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 -0- Blank lines between statament is certainly not pythonesque ;-) -1- Why not chose '/' as delimiter? -2- You may check there is a single delimiter. -3- You may consider a user-friendly (eg ''), rather than a programmer-friendly ("0,0") code for breking the loop. It'd cost you a pair of lines of code. For instance: FRACT = '/' def getUserInput(): while True: s1 = raw_input('Enter fraction as N/D or enter to exit > 1') if s1 == '': return None # will trigger exit in main nDelims = s1.count(FRACT) # only 1 is valid if nDelims == 1: n,d = s1.split(FRACT) return int(n),int(d) # remaining issue print 'invalid user input' getUserInput() An issue issue remains, namely that what around FRACT may not be valid integers. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor