#转自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()
08-04
08-04
08-04
08-04
08-04
08-04
08-04