[Tutor] How to print something just after 3 attempts?

Walter Prins wprins at gmail.com
Tue Jul 17 21:47:39 CEST 2012


Hi Santosh,

On 17 July 2012 16:10, Santosh Kumar <sntshkmr60 at gmail.com> wrote:
> The question says "Modify the password guessing program to keep track
> of how many times the user has entered the password wrong. If it is
> more than 3 times, print “That must have been complicated.”

FWIW, I interpret the above to mean that the program should continue
to loop until the correct password is entered (or until stopped with
Ctrl-C), e.g.  just like the original, but with this twist, namely
that if the user entered a password more than 3 times (e.g. 4 *or
more* attempts) trying to authenticate, then in addition (prior to)
the "Welcome" message, it prints the "That must've been hard" message.
 This seems a slightly more parsimonious interpretation of the
question IMHO.  Under this interpretation, the following will do:


# Waits until a password has been entered.  Use control-C to break out with out
# the password

password = "foobar"
attempts = 0
while password != "unicorn":
    password = raw_input("Password:")
    attempts += 1

if attempts > 3: print "That must have been complicated..."
print "Welcome in"

For reference, I tracked down the tutorial and original program here:
http://ccis.athabascau.ca/html/courses/comp200n/python/node6.html

Hope that helps,

Walter


More information about the Tutor mailing list