[Tutor] a simple password program

Karl Pflästerer sigurd at 12move.de
Thu Mar 11 16:48:43 EST 2004


On 11 Mar 2004, Nick Noda <- simple_twist at adelphia.net wrote:

> 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. any help i would jump up and
>  down for.

Think what happens if after the third pass the password is still wrong:
The loop looks in the condition: password != "nabbers!" which is true,
so the loop starts a new cycle; count is bigger than three so it jumps
directly to the else part, prints its message and starts a new cycle ...
So you have to find a way to break the loop unconditionally: if you only
wanted to stop the loop `break' would be right, but you also want to
stop the programm; for that you could use a method from the sys module:
sys.exit().

You could write:

import sys
count = 0
password = ""

while password != "nabbers":
    if count < 3:    
        password = raw_input("Password: ")
        count += 1
    else:
        print "Too complicated, huh?"
        sys.exit()

print "Welcome inside."



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list