#转自https://www.jianshu.com/c/00c61372c46a网址
import getpass
userdb = {}
def register():
username = input('username: ')
if username in userdb: #判断用户名是否在字典中
print('%s already exists.' % username)
else:
password = input('password: ')
userdb[username] = password #将用户名和密码加入字典中
def login():
username = input('username: ')
password = getpass.getpass('password: ') #输入密码,密码不在屏幕上显示(getpass.getasss)
if userdb.get(username) != password:
print('login failed')
else:
print('login successful')
def show_menu():
cmds = {'0':register,'1':login}
prompt = """(0) register
(1) login
(2) exit
Please input your choice(0/1/2): """
while True:
choice = input(prompt).strip()[0] #输入的去除空格(strip()),取脚标为0的作为输入([0])
if choice not in '012':
print('Invalid inupt.Try again.')
continue
if choice == '2':
break
cmds[choice]() #调用上面定义的函数
if __name__ == '__main__':
show_menu()
python-利用字典模拟登陆(54)
最新推荐文章于 2023-05-10 00:44:25 发布
本文介绍了一个使用Python实现的简易用户注册和登录系统。系统通过字典存储用户信息,利用getpass模块安全地输入密码,提供了注册、登录和退出功能。
3674

被折叠的 条评论
为什么被折叠?



