from getpass import getpass
userdb = {}
def login():
username = input(‘username: ‘)
password = getpass(‘password: ‘)
if userdb.get(username) != password:
print(‘用户名或密码错误。’)
else:
print(‘登陆成功’)
def register():
username = input(‘username: ‘)
if username not in userdb:
password = getpass(‘password: ‘)
userdb[username] = password
else:
print(‘%s 用户已存在’ % (username))
def show_menu():
cmds = {‘0’:register, ‘1’: login }
prompt = ”’
(0) 新用户注册
(1) 登陆
(2) 退出
请选择(0/1/2):”’
while True:
choice = input(prompt).strip()[0]
if choice not in ‘012’:
print(‘无效输入,请重试。’)
continue
if choice == ‘2’:
break
cmdschoice
if name == ‘main‘:
show_menu()