【发布时间】:2023-01-29 12:25:40
【问题描述】:
我试图让用户输入用户名和密码,如果输入错误,程序必须反复要求用户输入用户名和密码,直到输入正确的用户名和密码
users = {
'admin': {'password': 'adm1n'},
'man': {'password': 'thing'},
'cool': {'password': 'guy'}
}
while True:
user_input = input('Enter your username: ')
for username, data in users.items():
if user_input == username:
password = input('Enter the password: ')
if password == data['password']:
print('Welcome')
break
else:
print('The password you have entered is incorrect')
continue
else:
print('The username does not exist')
continue
break
第一个 if 语句没有选择用户“man”和“cool”。它只是拿起“管理员”
如果我输入“admin”然后输入不正确的密码,“显示用户名不正确”,应该显示“密码不正确”
我该如何解决?
【问题讨论】:
-
你有问题吗?
标签: python dictionary for-loop while-loop