[Tutor] passwd.py

peter hodgson py at humnet.ucla.edu
Fri Apr 2 09:25:49 EST 2004

thanks karl; i'm back again with that pesky passwd.py;
'break' works fine; still some trouble with sys.exit()

i've manually indented, instead of tabbing; o.k.?

#Modify the password guessing program to keep track of how many times
# the user has entered the password wrong. If it is more than 3 times,
# print ``three times, you're out!''

import sys                                  #no "welcome.." after else:

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 var
   if current_count < count:
      print "no, stupid! try again;"
      print "three times, you're out!"
      break                                 #double indent;breaks loop
sys.exit(1)                                 #no good for "unicorn"
print "welcome in"

#without 'count': keep asking for password until the jerk gets it right;
#with count: he only gets three chances;

#sys.exit() [per w.chung, core PYTHON PROGRAMMING];
# indented twice fails to stop "welcome in";
# indented once causes exit after first incorrect password;
# not indented works fine with wrong passwds;
#  but also kicks out 'unicorn' w/ "three times, you're out!"

More information about the Tutor mailing list