AccountU = ['Cam', 'Copper']
AccountP = ['Pop1234', 'What?']
def LoginA():
print ("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print ("A few clicks away from your selection!")
UserE = input("Username: ")
if UserE in AccountU:
for position, UserE in enumerate(AccountU):
PasswordF = AccountP[position]
PassE = input("Password: ")
if PasswordF == PassE:
Menu()
else:
print ("Your account is not working.")
LoginA()
在这段代码中,我试着让它输入用户名,然后找出位置。这应该允许它与密码交叉检查,因为它们将在同一时间输入。但是,它从一开始就交叉检查,直到检查正确的位置
例如,如果我为用户名输入“Copper”,那么它会询问密码两次,即使我输入了“What?”。我认为这可能与“for position,UserE in enumerate(AccountU):”有关,但我对“for position,UserE in enumerate(AccountU):”非常困惑
要求输入两次密码的证明
问题在于
设置if条件并检查用户输入的密码
使用字典是更好的选择
您不需要循环查看
AccountU
中的每个条目—您知道要查找哪个用户名,只需要相应的密码。如果使用.index()
方法,可以在AccountU
列表中找到用户名的索引。然后,假设AccountP
与AccountU
直接匹配,您可以在同一索引中找到正确的密码,如下所示:不过,最好使用字典来存储用户名和密码。这将确保正确的用户与正确的密码匹配。下面是一个如何工作的例子:
相关问题 更多 >
编程相关推荐