|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import json
- def get_stored_username():
- filename = 'username.json'
- try:
- with open(filename) as f:
- username = json.load(f)
- except FileNotFoundError:
- return None
- else:
- return username
- def get_new_username():
- username = input('What is your name? ')
- file = 'username.json'
- with open(file, 'w') as f:
- json.dump(username, f)
- return username
- def greet_user():
- username = get_stored_username()
- if username:
- answer = input(f"Are you jack?y/n ")
- if answer == 'y':
- print(f'welcome back {username}!')
- else:
- username = get_new_username()
- print(f"We'll remember you when you back,{username}!")
- else:
- username = get_new_username()
- print(f"We'll remember you when you back,{username}!")
- greet_user()
复制代码
这个程序主要作用就是验证用户是否存在,存在就问候,不存在就提示他,唐他输入用户名,存为老用户。
有一个问题就是,如果已经存在了老用户,然后输入的名字不是老用户的名字,然后提示你输入新用户,
此时,老用户的名字就被覆盖了,如何才能不覆盖,即使新用户注册了,下次验证时都能验证上。老用户不会消失。
|
|