用戶登錄系統
程序示例:
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

浙公網安備 33010602011771號