import json
def get_store_username():
# 获取用户名
filename ='username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
#存储用户名
username = input('What is your name?')
filename = 'username.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
return username
def greet_user():
#欢迎用户
username = get_store_username()
if username:
check = input('Are you' + username + '?(y/n)')
if check == 'y':
print('Welcome back,' + username + '!')
if check == 'n':
get_new_username()
print("We'll remember you when you come back," + str(username) + '!')
else:
username = get_new_username()
print("We'll remember you when you come back," + username + '!')
greet_user()