variable not being added, help

Bernd Zimmermann zimmermann.edv at t-online.de
Thu Nov 28 04:55:42 EST 2002


"stibbs" <stibbs at nothanks.foo> schrieb im Newsbeitrag
news:pan.2002.11.28.08.36.04.997032.60987 at nothanks.foo...
> i'm 100% sure the mistake is on my part, but i can't seem to figure out
> what the problem is, as you can see by my comment in the code i tried 2
> different ways. I am trying to add to pw_attempts and have it stick.
>
>
> #!/usr/local/bin/python
>
> import sys
>
> username = raw_input("username?\n")
>
> def getpass():
> try:
> pw_attempts
> except NameError:
> pw_attempts = 0
>
> password1 = raw_input("password?\n")
> password2 = raw_input("re-enter password.\n")
> #pw_attempts = pw_attempts + 1
>
> if password1 != password2:
> pw_attempts = pw_attempts + 1
>
> if pw_attempts > 2:
> sys.exit()
> print pw_attempts
> print "your password does not match! Try again\n"
> getpass()
> else:
> pass
> getpass()
> print "Passwords Matched!"

The recursive invocation of getpass() does not provide any information
about the current number of attempts.
As long as pw_attempts is not provided as parameter or defined
global, it is locally to getpass() and  starts with a value of 0
each time getpass() is invoked.

this should work:

def getpass(pw_attempts=0): # provide a start value
    # ...
    if pw_attempts > 2:
        sys.exit()
    # ...
    getpass(pw_attempts) # start recursion with current value

Regards,
Bernd





More information about the Python-list mailing list