[Tutor] user input help

Alex Hall mehgcap at gmail.com
Wed Jan 5 21:22:49 CET 2011


On 1/5/11, Jason Staudenmayer <jasons at 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 at 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)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list