> -----Original Message----- > From: Alex Hall [mailto:mehg...@gmail.com] > Sent: Wednesday, January 05, 2011 3:23 PM > To: Jason Staudenmayer > Cc: tutor@python.org > Subject: Re: [Tutor] user input help > > > On 1/5/11, Jason Staudenmayer <jas...@adventureaquarium.com> wrote: > > Hi all, I'm pretty new to programming in general and > figured I'd try out > > python. > > I'm working on a small program to add users to a sqlite db. > The problem I'm > > having it dealing with the user input, I'd like to be able > to repeat the > > function to get the input if the user doesn't accept it. > > > > here's the code I have now: > > > > def promptInput(): > > """ Get employee data from user running this program""" > > > > lname = raw_input("Please enter employees last name\n") > > fname = raw_input("Please enter employees first name\n") > > email = raw_input("Please enter employee email address > (or press enter > > to \ > > leave blank)\n") > > result = (lname, fname, email) > > return result > > > > def getEmplyInfo(): > > # get the data from input > > result = promptInput() > > # print the data so the user can check and verify spelling > > print "Is the following info correct [y/n]\n%s, %s %s" > % (result[1], \ > > result[0], result[2]) > > check = raw_input() > > #see if the user needs to make corrections to the data > he entered > > if check == "y": > > print "this check is done so we can add user" > > print "%s, %s %s" % (result[1], result[0], result[2]) > > else: > > check = "" > > promptInput() > > > > The if else loop is were I'm loosing it. If I select n it > will ask for the > > input > > again but only once. If on the second time around I enter n > to re-do it just > > exits. > This is because the function is done once it detects the y or n, so > after you enter the n, one of those if/else statements has fired, and > the function has nothing else to do. You will want a while loop, > something like: > > repeat=True > while repeat: > answer=raw_input("Is the data okay?") > if answer=="y": repeat=False > else: > promptInput() > repeat=True > > Anyway, something along those lines. Look in the manual for while > loops. Basically, they are a way to repeat an action until a condition > is met. You will also run across for loops, which are mostly used for > repeating an event a set number of times. You can use them > interchangeably, but they each have situations where one works better > than the other, and you want a while loop here. > > > > > > > > Thanks > > > > Jason > > > > > > > > ..·><((((º> > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehg...@gmail.com; http://www.facebook.com/mehgcap >
That works great!! I forgot about while loops. It's been a while since I've used then. Thanks much Jason _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor