[Tutor] a simple password program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Mar 11 16:52:57 EST 2004



On Thu, 11 Mar 2004, Nick Noda wrote:

>     i've only been using python for a few days now and i was following a
> non-programers tutorial. one of the excercises was to make a password
> program that after 3 incorrect guesses at the password tells the user
> that the pass was incorrect. this is what i got:
>
> password = "foobar"
> count = 0
> while password != "nabbers":
>     if count < 3:
>         password = raw_input("Password:")
>         count = count + 1
>     else:
>         print "Too complicated, huh?"
> print "Welcome inside."
>
>     i can get it to say "Welcome inside." if the pass is correct, but if
> the pass is guessed wrong three times it just keeps looping "Too
> complicated, huh?" i was wondering if it is supposed to do that or if
> there is a way that i can get it to say it once then quit.


Hi Nick,


Yes, you probably want to "break" out of the loop.  Here's an example:

###
>>> def yesOrNo():
...     while True:
...         print "y/n?",
...         choice = raw_input()
...         if choice in ('y', 'n'):
...             break
...     return choice
...
>>> yesOrNo()
y/n?blah
 y/n?yes
 y/n?y

'y'
###



Hope this helps!




More information about the Tutor mailing list