编写login.py脚本,实现以下目标:
- 支持新用户注册,新用户名和密码注册到字典中
- 支持老用户登陆,用户名和密码正确提示登陆成功
- 主程序通过循环询问进行何种操作,根据用户的选择,执行注册或是登陆操作
#!/usr/local/bin/python3 # -*- conding : utf-8 -*- """ @Time : 19-3-16 上午10:45 @Author : xq @Software : PyCharm @File : userlogin.py @Description : """ import getpass userdb = {} def register(): username = input('item to username: ').strip() if username not in userdb: password = input('item to userpasswd: ').strip() userdb[username] = password #key不在字典就添加 else: print('%s already exitsts!!'% username) def login(): username = input('username: ') password = getpass.getpass('password: ') # if username in userdb and userdb[username] == password: # if userdb.get(username) == password: if userdb[username] == password: #判断键值等不等 print('Login successful') else: print('login failed') def show_menu(): prompt = """(0)register (1)login (2)quit please choices(0/1/2) : """ while True: cmd = {'0': register,'1': login} choice = input(prompt).strip() # 去除用户输入的两端的空白字符 if choice not in ['0','1','2']: print("Invalid input,please try again") continue if choice == '2': break cmd[choice]() if __name__ == "__main__": show_menu()