[Tutor] passwd.py

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Apr 15 03:47:37 EDT 2004

> import sys                                  #no "welcome.." after
> passwd = "foobar"                           #a dummy passwd
> count = 3
> current_count = 0
> while passwd != "unicorn":
>    current_count = current_count + 1
>    passswd = raw_input("Passwd: ")          #P..as text; p.. is a
>    if current_count < count:

A wee bug here I think. This will fail on the
third attempt - ie your users only get two guesses!
You need to check for (current_count <= count) I think.

>       print "no, stupid! try again;"
>    else:
>       print "three times, you're out!"
>       break                                 #double indent;breaks

I dont think you need break and sys.exit. If you puit the
exit instead of the bbreak it should work.

> sys.exit(1)                                 #no good for "unicorn"

This gets called after you exit the loop regardless of
whether it was succesfully or not.

> print "welcome in"

> #sys.exit() [per w.chung, core PYTHON PROGRAMMING];
> # indented twice fails to stop "welcome in";

Thats because you hit break first and it exits the loop

> # indented once causes exit after first incorrect password;

Thats because its part of the loop and executes after the
if statement each time.

> # not indented works fine with wrong passwds;
> #  but also kicks out 'unicorn' w/ "three times, you're out!"

I think thats the <= bug I mentioned earlier.


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list