Basic question from pure beginner
Scott David Daniels
Scott.Daniels at Acm.Org
Wed Jul 1 13:47:47 EDT 2009
Charles Yeomans wrote:
> Let me offer a bit of editing....
> 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
And even simpler:
PASSWORD = "qwerty"
MAXRETRY = 3
for attempt in range(MAXRETRY):
if raw_input('Enter your password: ') == PASSWORD:
print 'Password confirmed'
break # this exits the for loop
print 'Access denied: attempt %s of %s' % (attempt+1, MAXRETRY)
else:
# The else for a for statement is not executed for breaks,
# So indicates the end of testing without a match
raise SystemExit # Or whatever you'd rather do.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list