首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python用户名和密码程序

Python用户名和密码程序
EN

Code Review用户
提问于 2017-05-27 23:05:51
回答 5查看 134.2K关注 0票数 10

下面的代码可以简化吗?我认为我创建的程序是非常过度和相当缓慢的。程序运行良好,但我想了解这是否真的可以更好地编程。

我已经对代码的每一行进行了评论。任何形式的反馈都将是伟大的,因为我只编程了几个星期了。

代码语言:javascript
运行
复制
# Python 3.4.3. Using MacOS (Version 10.12.5)
# Username and Password...
# The programs purpose: A user must enter the correct username and password for a site called FaceSnap...
# The correct username is elmo and the correct password is blue.


userName = input("Hello! Welcome to FaceSnap! \n\nUsername: ") #Ask's the User for Username input
password = input("Password: ") # Ask's the user for their password


count = 0 # Create a variable, to ensure the user has limited attempts at entering their correct username and password
count += 1 # The user has already had one attempt above, therefore count has been incremented by 1 already.


while userName == userName and password == password: # The Input will always lead to this while loop, so we can see if their username and password is wrong or correct.


    if count == 3: # Counter, to make sure the user only gets a limited number (3)of attempts
        print("\nThree Username and Password Attempts used. Goodbye") # Lets the user know they have reached their limit
        break # Leave the Loop and the whole program


    elif userName == 'elmo' and password == 'blue': # The userName and password is equal to 'elmo' and 'blue', which is correct, they can enter FaceSnap!
        print("Welcome! ") # Welcomes the User, the username and password is correct
        break # Leave the loop and the whole program as the username and passowrd is correct


    elif userName != 'elmo' and password != 'blue': # The userName and password is NOT equal to 'elmo' and 'blue', the user cannot enter FaceSnap
        print("Your Username and Password is wrong!") # Lets the user know that the Username and password entered is wrong.
        userName = input("\n\nUsername: ") # Requests the user to have another attempt at entering their correct username
        password = input("Password: ") # Requests the user to have another attempt at entering their correct password
        count += 1 # Increments the count by 1
        continue # Continue, as the user hasn't managed to get their username and password correct yet


    elif userName == 'elmo' and password != 'blue': # The userName is equal to 'elmo', but password is NOT equal to 'blue', the user cannot enter FaceSnap
        print("Your Password is wrong!") # Lets the user know that their password is wrong
        userName = input("\n\nUsername: ") # Requests the user to have another attempt at entering their correct username
        password = input("Password: ") # Requests the user to have another attempt at entering their correct password
        count += 1 # increments the count by 1
        continue # Continue, as the user hasn't managed to get their username and password correct yet


    elif userName != 'elmo' and password == 'blue': # The userName is NOT equal to 'elmo', however password is equal to 'blue', the user cannot enter FaceSnap
        print("Your Username is wrong!") # Lets the user know that their username is wrong
        userName = input("\n\nUsername: ") # Requests the user to have another attempt at entering their correct username
        password = input("Password: ") # Requests the user to have another attempt at entering their correct password
        count += 1 # Increments the count by 1
        continue # Continue, as the user hasn't managed to get their username and password correct yet
EN

回答 5

Code Review用户

回答已采纳

发布于 2017-05-27 23:59:59

以下几点:

  • 您可以使用if条件简化else循环。
  • 获取输入应该在while循环的开头,因为把它放在那里更符合逻辑。这是每次你重新启动时都会发生的事情。
  • 您可以将True的简单条件用于while循环。
  • 您不需要continue关键字,因为它位于循环的末尾。

代码语言:javascript
运行
复制
count = 0 
while True: 
    userName = input("Hello! Welcome to FaceSnap! \n\nUsername: ") 
    password = input("Password: ")
    count += 1
    if count == 3: 
        #tells user bye
        break #exit
    else:
        if userName == 'elmo' and password == 'blue':
            #let them in
            break #they are in, exit loop
        else:
            #tell them it is wrong and have them retry, stay in loop
票数 11
EN

Code Review用户

发布于 2017-05-28 08:07:00

注释

由于注释的过度使用,您的代码很难阅读。注释应该在代码中添加一些内容,而不是重复代码,或者告诉读者代码已经强烈暗示了什么。

使用散列

仅仅是散列很弱,但这是相当容易实现的。还要注意的是,md5容易受到哈希冲突攻击,不应该再使用了。如果您的平台支持它,使用SHA(3)-256。

这个应用程序的问题是,如果有人访问代码,他们可以用明文读取用户名和密码。最好先对密码进行散列,然后使用hashlib检查输入是否匹配。我在下面编写了您的代码的改进版本,以说明我的意思:

代码语言:javascript
运行
复制
from hashlib import md5
from getpass import getpass
import sys

print("Hello! Welcome to FaceSnap!") 

attempts = 0
check_username = "5945261a168e06a5b763cc5f4908b6b2"
check_password = "48d6215903dff56238e52e8891380c8f"
# These hashes have been generated earlier on.
# This is not how you would go about storing usernames and passwords,
# but for the sake of simplicity, we'll do it like this.

while True: 
    username = input("Username: ")
    password = getpass("Password: ")
    # Getpass will not echo input to the screen, so your password remains 
    # invisible
    print()

    if attempts == 3:
        sys.exit("Too many failed attempts.")

    if md5(username.encode().hexdigest() == check_username:
        if md5(password.encode().hexdigest() == check_password:
            print("Username and password entered correctly.")
            # Username and password match - do something here
        else:
            print("Password entered incorrectly.")
            attempts += 1
    else:
        print("Username entered incorrectly.")
        attempts += 1

使用氪星

实际上,您不会通过散列密码将密码存储在数据库中,而是使用专用的密钥推导函数 (如氪石 )。想到的第一个用于Python的密码库是PyCrypto,但是cryptography使您很难对自己开枪。

票数 4
EN

Code Review用户

发布于 2017-05-28 14:48:51

这将是一个很短的过程,但是当您开始编写count = 0,然后编写count += 1,在这里您可以轻松地编写count = 1

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/164359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档