Best way to do this?

Bruno Desthuilliers bdesth.tagada at tsoin-tsoin.free.fr
Sun Jan 18 10:43:23 EST 2004


Wil Schultz wrote:
(Wil, please don't top-post... corrected)
> 
> William Park wrote:
> 
>> Wil Schultz <newz at rekon.org.nospam> wrote:
>>
>>> One of the exercises asks for a program to ask for a password three 
>>> times. Surprisingly this took me much longer that it probably should 
>>> have so I am curious if the following is the easiest way this is 
>>> done. Thanks much!
>>>
>>> *************************************************
>>> #!/usr/local/bin/python
>>>
>>> count = 0
>>> password = "null"
>>>
>>> while count < 3:
>>>         count = count + 1
>>>         password = raw_input("what's the pass: ")
>>>         if password == "mypass":
>>>                 print "Access Granted"
>>>                 count = 3
>>>         else:
>>>                 if count < 3:
>>>                         print "Try Again"
>>>                 else:
>>>                         print "Too Bad"
>>> *************************************************
>>
>>
>>
>> For crying out loud, here's 3 times for you...
>>     password = raw_input("what's the pass: ")
>>     password = raw_input("what's the pass: ")
>>     password = raw_input("what's the pass: ")

>> Okay, maybe I should have said the program should ask for the password 
>> up to 3 times, exiting on a) the correct password b) the third incorrect 
>> password.
>> 
Beware of specs, programmers can have strange interpretations sometimes !-)

import sys
for i in range(3):
    if raw_input("what's the pass: ") == "mypass":
       print "Access granted"
       sys.exit(0)

print "Access denied"
sys.exit(1)

HTH
Bruno






More information about the Python-list mailing list