Basic question from pure beginner

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Jul 1 10:26:16 CEST 2009


sato.photo at gmail.com a écrit :
> Thank you for all of the help.  With your assistance and help from the
> Python Tutor mailing list I was able to come up with the following
> code:
> 
> password = "qwerty"
> correct_password_given = False
> guess = "0"

You could just use None here:

   guess=None


> count = 0
> while count != 3 and not correct_password_given :
>    guess = raw_input("Enter your password: ")
>    guess = str(guess)

IIRC, raw_input() already returns a string !-)

>    if guess != password:
>        print "Access Denied"
>        count = count + 1

Or:
          count += 1

>    else:
>        print "Password Confirmed"
>        correct_password_given = True
> 
> 
> If I understand it correctly, the function will continue to loop until
> either the count == 3 or until correct_password_give =  True,

There are many ways to write a same boolean test, and some are easier to 
understand. Your test could be written:

   while not (count == 3 or correct_password_given) :
      # proceed

Does it answer your question ?-)

As a general rule:

* not A and not B <=> not (A or B)
* not A or not B  <=> not (A and B)


> satisfying the two conditions of the assignment, which were to lock a
> user out after three failed password attempts and to print "Access
> Granted" and end the module if the correct password is given.

Well, given your snippet, execution indeed terminates after either 
successful 'login' or 3 failed attempts, but that's mostly because 
there's no code to execute after the while loop...




More information about the Python-list mailing list