help on ultra newbie program please

Andrew Walkingshaw andrew-usenet at lexical.org.uk
Wed May 21 12:46:58 EDT 2003


In article <HRNya.29$%s1.24774 at news.uswest.net>, cybernut wrote:
> # Waits until a password has been entered.  Use control-C to break out with
> out
> # the password
> 
> #Note that this must not be the password so that the
> # while loop runs at least once.
> password = "foobar"
> 
> #note that != means not equal
> while password != "unicorn":
>     password = raw_input("Password:")
> print "Welcome in"

Presumably this is the original program.

What you want is something like:

(program starts - hope the comments are useful)

# set password to be blank initially
password = ""
# and create a variable to count the number of failed login attempts
numfailures = 0

while numfailures < 3:
    password = raw_input("Password: ")
    if password == "unicorn":
        # success, so get out of the loop
        break
    else:
        # increment number of attempts by one
        numfailures = numfailures + 1

if numfailures == 3:
    # then the user failed to log in three times
    print "Access denied."
else:
    # numattempts < 3, so no more than two failed attempts 
    # therefore - allow access
    print "Welcome in."


This, when run, produces output which looks like -

adw27 at pond:~/siesta-zrw2o8-large-new 1014 $
:; python test.py 
Password: unicorn
Welcome in.
adw27 at pond:~/siesta-zrw2o8-large-new 1015 $
:; python test.py 
Password: asd
Password: asd
Password: wed
Access denied.

You might want to investigate the python-tutor at python.org mailing list:
it's full of people who like to help new programmers with this kind of
thing!

Hope this helps, 

- Andrew 

-- 
Andrew Walkingshaw | andrew-usenet at lexical.org.uk





More information about the Python-list mailing list