用戶登錄系統(tǒng)

程序示例:
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 # 每輸入一次密碼,計數(shù)器加 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
浙公網(wǎng)安備 33010602011771號