newbie password question

Sheila King usenet at thinkspot.net
Sat Mar 2 01:04:23 EST 2002


On 02 Mar 2002 05:35:14 GMT, etrofsouls at aol.com (EtrOFsouls) wrote in
comp.lang.python in article <20020302003514.13693.00001116 at mb-fw.aol.com>:
 
> password = "foobar"
> 
> while password != "unicorn":
>       password = raw_input ("Password:")
> print "Welcome In"
> 
> I understand all this (thank god), but I'm having trouble figuring out how to
> get it to keep track of the number of guesses and then print a message such as
> "Halt, you do not have access!" if the number of guesses exceeds the limit. I'm
> learning this thru on-line tutorials and this was given as an excersise. All I
> have to work with is variables and statements such as...while, if, elif and
> else. 

How about something like this?

password = "foobar"

validated = 0
for attempt in range(1, 6):
      password = raw_input ("Password:")
      if password == "foobar":
          validated = 1
          print "Welcome In"
          break
if not validated:
    print "Sorry, Charlie!"



The variable validated keeps track of whether the person has yet managed to
successfully give the correct password. (In this example, the correct
password is "foobar".) The for loop lets the user try five times (try this
at an interactive prompt:

print range(1,6)

It will return a list of the integers [1, 2, 3, 4, 5]. So basically, this
loop will try for times 1, 2, 3, 4 and 5. The user gets five chances to get
the correct password..

If they manage to get the correct password, the validated flag is set to
"true" (i.e. the number 1), they are greeted with a "welcome", and the loop
breaks. If the loop runs all five times, and after that the validated flag
is still false, "Sorry, Charlie!".

Hope this helps,

-- 
Sheila King
http://www.thinkspot.net/sheila/

"When introducing your puppy to an adult cat,
restrain the puppy, not the cat." -- Gwen Bailey,
_The Perfect Puppy: How to Raise a Well-behaved Dog_






More information about the Python-list mailing list