开发者

Can I improve on the current Python code?

I am just starting out with Python and decided to try this little project from Python Wiki:

Write a password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print You have been denied access. and terminate the program. If the password is correct, print You have successfully logged in. and terminate the program.

Here's my code. It works but it just doesn't feel right with these loop breaks and nested if statements.

# Password Guessing Program
# Python 2.7

count = 0

while count < 3:
    password = raw_input('Please enter a password: ')
    if password != 'SecretPassword':
        count = count + 1;
        print 'You have entered invalid password %i times.' % (count)
        if count == 3:
            print 'Access开发者_运维知识库 Denied'
            break
    else:
        print 'Access Granted'
        break


You can replace your while loop with the following function:

def login():
    for i in range(3):
        password = raw_input('Please enter a password: ')
        if password != 'SecretPassword':
            print 'You have entered invalid password {0} times.'.format(i + 1)
        else:
            print 'Access Granted'
            return True
    print 'Access Denied'
    return False

You may also want to consider using the getpass module.


I'm not against the "imperative" feel of loop/if, but I would separate your "business logic" from your "presentation":

count = 0

# Business logic
# The correct password and the maximum number of tries is placed here
DENIED, VALID, INVALID = range(3)
def verifyPassword(userPassword):
    global count
    count += 1

    if count > 3:
        return DENIED
    elif password == 'SecretPassword':
        return VALID

    return INVALID

# Presentation
# Here you do the IO with the user
check = INVALID
while (check == INVALID):
    password = raw_input('Please enter a password: ')
    check = verifyPassword(password)

    if check == INVALID:
        print 'You have entered invalid password %i times.' % (count)
    elif check == VALID:
        print 'Access Granted'
    else # check == DENIED
        print 'Access Denied'


granted = False # default condition should be the least dangerous
for count in range(3):
    password = raw_input('Please enter a password: ')
    if password == 'SecretPassword': # no need to test for wrong answer
        granted = True
        break
    print 'You have entered invalid password %i times.' % (count+1) # else

if granted:
    print 'Access Granted'
else:
    print 'Access Denied'


You can bring the if statement out of the while loop.

# Password Guessing Program
# Python 2.7

count = 0
access = False

while count < 3 and not access:
    password = raw_input('Please enter a password: ')
    if password != 'SecretPassword':
        count += 1
        print 'You have entered invalid password %i times.' % (count)
    else:
        access = True

if access:
    print 'Access Granted'
else:
    print 'Access Denied'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜