下面的代码可以简化吗?我认为我创建的程序是非常过度和相当缓慢的。程序运行良好,但我想了解这是否真的可以更好地编程。
我已经对代码的每一行进行了评论。任何形式的反馈都将是伟大的,因为我只编程了几个星期了。
# 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发布于 2017-05-27 23:59:59
以下几点:
if条件简化else循环。while循环的开头,因为把它放在那里更符合逻辑。这是每次你重新启动时都会发生的事情。True的简单条件用于while循环。continue关键字,因为它位于循环的末尾。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发布于 2017-05-28 08:07:00
由于注释的过度使用,您的代码很难阅读。注释应该在代码中添加一些内容,而不是重复代码,或者告诉读者代码已经强烈暗示了什么。
仅仅是散列很弱,但这是相当容易实现的。还要注意的是,md5容易受到哈希冲突攻击,不应该再使用了。如果您的平台支持它,使用SHA(3)-256。
这个应用程序的问题是,如果有人访问代码,他们可以用明文读取用户名和密码。最好先对密码进行散列,然后使用hashlib检查输入是否匹配。我在下面编写了您的代码的改进版本,以说明我的意思:
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使您很难对自己开枪。
发布于 2017-05-28 14:48:51
这将是一个很短的过程,但是当您开始编写count = 0,然后编写count += 1,在这里您可以轻松地编写count = 1。
https://codereview.stackexchange.com/questions/164359
复制相似问题