[Tutor] a simple password program

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Mar 12 12:26:39 EST 2004


>    i've only been using python for a few days now and i was
> following a non-programers tutorial. one of the excercises
> was to make a password program that after 3 incorrect
> guesses at the password tells the user that the pass was incorrect.

Hmm, I think Josh needs to do some work on his tutor since this
always seems to be the exercise that stumps newbies! :-)

> count = 0
> while password != "nabbers":
>     if count < 3:
>         password = raw_input("Password:")
>         count = count + 1

One thing that beginners often seem to miss is that boolean
expressions (ie tests) can be combined using logical
operators(and, or, not, etc).

Thus you can check

while (password != 'nabbers') and (count < 3):

This can greatly simplify the logic inside the loop.

Its exactly like you combine arithmetic operations together
using parentheses.

Take a look at my tutorial pages on Raw Materials, under Boolean
Values
and also in the Branching topic where it discusses combining tests.

Remeber that computers need to be told *exactly* what to do, they
can't infer things from what you've already told them...

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list