我有以下代码,在登录功能中,输出是错误的(逻辑错误)。它基本上打印“无效的用户名和密码”,直到它到达正确的用户名和密码,然后打印“正确登录”。
ERRONEOUS OUTPUT :
例如,使用测试数据:user3和pass3,输出为:
*****LOGIN SCREEN******
Username: user3
Password: pass3
invalid username or password
invalid username or password
correct login
>>>
这是代码,参考LOGIN函数:
usernames=["user1","user2","user3"]
passwords=["pass1","pass2","pass3"]
def main():
mainmenu()
def mainmenu():
print("****MAIN MENU****")
print("=======Press L to login :")
print("=======Press R to register :")
choice1=input()
if choice1=="L" or choice1=="l":
login()
elif choice1=="R" or choice1=="r":
register()
else:
print("please make a valid selection")
usernames=["user1","user2","user3"]
passwords=["pass1","pass2","pass3"]
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
print("correct login")
else:
print("invalid username or password")
def register():
print("*****REGISTRATION****")
username=input("Enter a username:")
password=input("Enter a password:")
users_pass[username] = password
answer=input("Do you want to make another registration?")
if answer=="y":
register()
else:
registration_details()
def registration_details():
print(usernames)
print(passwords)
main()
我在找 a)解决问题的解决方案并修复,以便在找到正确的用户名和密码对时打印“正确登录”,而不是循环并打印每个用户名
b)解释关于为什么缩进或其他错误在这里不起作用 - 因为逻辑(用VB.Net这样的语言)是合理的。
如果没有额外的代码(和一个非常简单的修复)可以修复问题,我更喜欢它,但也许最优雅的解决方案是标志。也就是说,如果问题不仅仅是缩进或类似于我错过的东西。
答案 0 :(得分:3)
您可以利用异常以这种方式写入登录。
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
try:
index = usernames.index(username)
if password == passwords[index]:
print("correct login")
else:
print("invalid username or password")
except:
print("invalid username or password")
答案 1 :(得分:2)
由于登录功能中的else语句,您会发现这种情况。基本上你的代码当前在该函数中所做的是循环,检查用户名和密码是否等于当前值(即比较user1 == user2)如果它们不相等则会自动打印无效的用户名。
相反,您应该等到比较所有值以打印无效的用户名或密码消息。另外 - 一旦找到有效值,您可以添加一个中断来停止for循环,而不是继续遍历您的值。您的登录功能如下所示:
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
found = False
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
print("correct login")
found = True
break
if found == False:
print("invalid username or password")
这将只为您提供1个正确登录或无效用户名或密码的实例。
答案 2 :(得分:1)
在你的login()函数中,你打印列表的每个元素,所以你可以在循环后打印:
def login():
print("*****LOGIN SCREEN******")
username=input("Username: ")
password=input("Password: ")
correct_login = False
for index_of_current_user, current_user in enumerate(usernames): #enumerate allows to you to go throw the list and gives to you the current element, and the index of the current element
if username == current_user and passwords[index_of_current_user] == password: #since the two list are linked, you can use the index of the user to get the password in the passwords list
correct_login = True
break
if(correct_login):
print("correct login")
else:
print("invalid user name or password")