我试图设计一个程序来匹配用户名和他们的密码。我已经使用了两个独立的列表,但现在我在想2d数组或字典会更好吗? 欢迎任何建议。谢谢
def login():
official_users=['dan','bob','bill']
official_passwords=['snow','golf','dogs']
username=input("Enter your user name: ")
while username in official_users:
print("You are user number:
",official_users.index(username)+1)
position=(official_users.index(username))
print(position)
attempts=0
while attempts<3:
password=input("Enter your password: ")
if password == official_passwords[position]:
print("Log in successfull")
print("Hello",username)
login()
else:
print("Incorrect password")
attempts+=1
print("Attempts left: ",3-attempts)
if attempts==3:
print("Too many attempts!")
login()
print("Unknown User")
login()
login()
希望有帮助
def login():
official = {'dan': ['snow', 1],
'bob': ['golf', 2],
'bill': ['dogs', 3]
}
username=input("Enter your user name: ")
if username in official:
print("You are user number: ",official[username][1])
attempts=0
while attempts<3:
password=input("Enter your password: ")
if password == official[username][0]:
print("Log in successfull")
print("Hello",username)
login()
else:
print("Incorrect password")
attempts+=1
print("Attempts left: ",3-attempts)
if attempts==3:
print("Too many attempts!")
login()
print("Unknown User")
login()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。