Best way to do this?

Mel Wilson mwilson at the-wire.com
Mon Jan 19 08:31:28 EST 2004


In article <100jg9er3qak890 at corp.supernews.com>,
Wil Schultz <newz at rekon.org.NOSPAM> wrote:
>I am reading the "Non-Programmers Tutorial For Python" by Josh Cogliati
>http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html
>
>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.
>
>*************************************************
>#!/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"
>*************************************************

The "obscure" for/else can help:


def get_password (prompt):
    for i in (1, 2, 3):
        password = raw_input (prompt)
        if password == 'mypass':
            return True
        else:
            print "Try Again"
    else:
        print "Too Bad"
        return False



        Regards.        Mel.



More information about the Python-list mailing list