用户登录系统

程序示例:
users = {"user1": "123456", "user2": "123456", "user3": "123456"}
blacklist = ["user4", "user5", "user6"]
count = 1
while True:
name = input("Enter your name (q to quit): ")
if name == 'q':
break
else:
# 用户名存在且不在黑名单内
if name in users.keys():
for i in range(3):
password = input("Enter your password:")
count += 1 # 每输入一次密码,计数器加 1
if password == users[name]:
print("Welcome " + name + "!")
break
else:
if count < 4:
print("Sorry " + name + "'s password is incorrect. Enter a different password: ")
# 输出三次密码都不对,count 初始值为 1,此时为 4
if count == 4:
print("Sorry " + name + "'s password is incorrect. You have no chance anymore!")
# 在黑名单里
elif name in blacklist:
print("Sorry " + name + ", you are in blacklist!")
# 用户名不存在,可以注册
else:
print("Sorry " + name + ", you are not registered! You can register now!")
password = input("Enter your password:")
print("Welcome " + name + "! You are now logged on!")
users[name] = password
另一种写法:
程序示例:
users = {"user1": {"password": 12345, "status": True}, "user2": {"password": 12345, "status": True},
"user3": {"password": 12345, "status": False}}
for i in range(3):
name = input("Enter your name: ")
password = int(input("Enter your password: "))
# 用户名存在且不在黑名单内,且用户名输入正确
if name in users.keys() and users[name]["password"] == password and users[name]['status']:
print("Welcome " + name + "!")
break
# 用户名存在且在黑名单内
elif name in users.keys() and not users[name]['status']:
print("Sorry " + name + ", you are prohibited from logging in!")
break
elif name in users.keys() and password != users[name]['password']:
print("Sorry " + name + ", your password does not match!")
elif name not in users.keys():
print("Sorry " + name + ", you are not logged in!")
break
浙公网安备 33010602011771号