1
方法 (1)
database = {" ": " "} while True: choice = int(input("网站登录界面\n1:登录\n2:注册\n3:退出\n请选择:")) if choice == 1: print("欢迎来到登录界面") name = input("请输入用户名:") password = input("请输入密码:") a = {name: password} for i, j in database.items(): if a == {i: j}: print("登录成功") break if a != {i: j}: print("账号或密码错误,请重新输入") if a == {i: j}: break if choice == 2: while True: print("欢迎来到注册界面\n不需要注册输入:0") name = input("请输入用户名:") if name == "0": print("退出注册\n") break for i in database.keys(): if name == i: print("此账户已被注册\n") break if name != i: password = input("请输入密码:") if name != i: database.setdefault(name, password) print("注册成功") break if choice == 3: print("退出成功") break

 

 

 

 

 

方法 (2)
dic = {}
while True:
print("1.登陆")
print("2.注册")
print("3.退出")
print("请选择:")
num = int(input())
if num == 1:
print("欢迎来到登陆页面")
username = input("请输入用户名:")
passwd = input("请输入密码:")
passwd1 = dic.get(username)
if passwd1==None:
print("没有此用户")
else:
if passwd1 == passwd:
print("登陆成功")
else:
print("登陆失败")
elif num == 2:
print("欢迎来到注册页面")
username = input("请输入用户名:")
passwd = input("请输入密码:")
if dic.get(username) == None:
dic[username]=passwd
print("注册成功")
print(dic)
else:
print("此用户已经存在,注册失败")
elif num == 3:
break