Basic question from pure beginner

Charles Yeomans charles at declareSub.com
Wed Jul 1 12:26:41 EDT 2009


Let me offer a bit of editing.

First, using the condition count != 3 is perhaps risky.  A mistake or  
a change in logic in the loop body might result in an infinite loop.   
So instead I suggest

while count < 3...

Second, I'd suggest storing the value 3 in a variable with a name that  
describes it.

MaxAttempts = 3
while count < MaxAttempts

This suggests that you change the name 'count' to 'attemptcount'.

The variable 'guess' is used only inside the loop, so I suggest  
removing the assignment outside the loop.

Finally, I'd remove correct_password_given from the loop test, and  
replace it with a break statement when the correct password is entered.

password = "qwerty"
correct_password_given = False
attemptcount = 0
MaxAttempts = 3
while attemptcount < MaxAttempts:
   guess = raw_input("Enter your password: ")
   guess = str(guess)
   if guess != password:
       print "Access Denied"
       attemptcount = attemptcount + 1
   else:
       print "Password Confirmed"
       correct_password_given = True
       break


Charles Yeomans

On Jul 1, 2009, at 3:58 AM, sato.photo at gmail.com wrote:

> 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"
> count = 0
> while count != 3 and not correct_password_given :
>   guess = raw_input("Enter your password: ")
>   guess = str(guess)
>   if guess != password:
>       print "Access Denied"
>       count = 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,
> 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.  Am I
> understanding this correctly?
>
> Thanks!
>
> On Jul 1, 12:06 am, alex23 <wuwe... at gmail.com> wrote:
>> On Jul 1, 3:38 pm, "sato.ph... at gmail.com" <sato.ph... at gmail.com>
>> wrote:
>>
>>> I have been able to make the module quit after entering a password
>>> three times, but can't get it to quit right away after the correct  
>>> one
>>> is entered.
>>
>> Not with the code you pasted, you haven't. There's a missing colon on
>> line 7 & line 9 isn't indented properly. It's always best if we're
>> referring to the same source when trying to help with a problem.
>>
>> The problem you're having, though, has to do with the while loop and
>> the fact that it's only checking one condition: has the 'count'
>> counter been incremented to 3. What you need to do is _also_ check  
>> for
>> the success condition:
>>
>> is_confirmed = False
>> while count != 3 or is_confirmed:
>>   guess = raw_input("Enter your password: ")
>>   guess = str(guess)
>>   if guess != password:
>>     print "Access Denied"
>>     count = count + 1
>>   else:
>>     print "Password Confirmed"
>>     is_confirmed = True
>>
>> This also provides you with a nice boolean that shows whether or not
>> the password was confirmed, which may be useful for latter code.
>>
>> However, whenever you want to loop a set number of times, it's  
>> usually
>> better to use a 'for' loop instead:
>>
>> PASSWORD = 'qwerty'
>> MAXRETRY = 3
>> for attempt in xrange(MAXRETRY):
>>   guess = str(raw_input('Enter your password: '))
>>   is_complete = guess == PASSWORD
>>   if is_complete:
>>     print 'Password confirmed'
>>     break # this exits the for loop
>>   else:
>>     print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
>>
>> This saves you from the burden of creating, incrementing & testing
>> your own counter.
>>
>> If there's anything here that isn't clear, please don't hesitate to
>> ask.
>
> -- 
>




More information about the Python-list mailing list