[Tutor] password,while loops,

alan.gauld@bt.com alan.gauld@bt.com
Tue, 13 Aug 2002 18:05:24 +0100


> It took me about 20 minutes to write this program(I
> dare to call this a program)and it worked ok.

Well done!

In the interests of good practice you could modify 
it very slightly as I've shown below.

> =======================================================
> password = "foobar"
> a=0
> 
> while password != "unicorn":
>     print "You've guessed",a,"times."
>     password = raw_input("Password?:")
      a=a+1

We don';t need to test if passwd == unicorn coz 
the while does that for us. Just increment the 
counter secure that the loop will stop when it needs to.

  if password=="unicorn":
      print "Welcome in, you took ", a, "attempts."

#################

Now when you come to tackle the second part of the 
challenge(testing if there were >3 attempts its a 
little easier because you've separated the getting 
the count part(inside the lopop) from the display
part.

In programming its nearly always a good idea to 
separate displaying results from the calculation 
part of the program.

But your version was perfectly fine since it did 
do what you wanted.

Alan G.