[Tutor] count

Michael P. Reilly arcege@shore.net
Fri, 13 Oct 2000 12:43:42 -0400 (EDT)


> 
> When I run this the last 'print' in the while block prints even when the 
> correct password is entered. I thought the lines in the while block only 
> executed if the while constraints were met. How would I make the last print 
> not show when the proper pasword is entered?thanks cmn
> 
> 
> count = 0
> print "Halt!"
> input = raw_input("Who goes there? ")
> while input != "Chris":
>     print "Please move away from my machine!",input,"!!"
>     input = raw_input("Who goes there? ")
>     count = count + 1
>     print "You have fondled my keys",count,"too many times!"
> print "Chris, an erroneous password was entered",count,"times!"
> print "WELCOME TO THE MACHINE, MASTER!"

The common idiom for this kind of loop is to have the raw_input at the
end of the loop:

input = raw_input("Who goes there? ")
while input != "Chris":
    count = count + 1
    if count > 1:
      print "You have fondled my keys", count, "too many times!"
    print "Please move away from my machine!", input, "!!"
    input = raw_input("Who goes there? ")
print "Chris, ...

This probably does what you want.

  -Arcege

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------