Newbe: password program

andy andy at eastonwest.co.uk
Sat Jan 18 21:15:38 CET 2003


pavs,

Python really is cool, isn't it.  Keep playing with it, the rewards are 
immense!

Some thoughts.

In your while loop, the statement

    print "Wrong Password"

will always be executed the first time.  If that's not what you want, you 
could solve it one of several ways:

1) Preload the password variable with raw_input()

count=0
password=raw_input("Password:")
while password != "unicorn":
    print "Wrong Password"
    password=raw_input("Re-enter password")
    count+=1

2) Check that the password is not the default, within the loop

count=0
password="default"
while password != "unicorn":
    if password <> "default":
        print "Wrong Password"
    password=raw_input("Password")
    count+=1

3) Control the loop with a flag 

count=0
gotit=0
while not gotit:
    password = raw_input("Password:")
    if password == "unicorn":
        gotit=1
    else:
        print "Wrong password"
        count+=1

I'm sure some of the guru's could code it differently too... remember, there's 
more than one way to, er, skin a python!

-andyj


On Saturday 18 Jan 2003 1:27 pm, pavs wrote:
> #Modify a password guessing program to keep track of how
> #many times the user has entered the pasword wrong.
> #If, it is more than 3 time, print "That must have been complicated."
>
> # ------------------------------------------------------------------
>
> #Waits untill password has been entered. Use Control -C to break out
> without # the password
>
> #Note that this must not be the password so that the while loop runs
> atleast once
>
> password = "pavs"
> count = 0
>
> while password != "unicorn":
>     print "Wrong Password"
>     password = raw_input("Password:")
>     count = count + 1
>
>
> print "Welcome in"
> if count > 3:
>     print "That must have been hard"
>
>
>
>
> ---------------------------------------------------------------------------
>-----
>
> :)
>
> I think the program works.
>
> If so, than this would be my first workable program without outside help :)
>
> Python is so much FUN!
>
> Cheers,
> pavs






More information about the Python-list mailing list