函数之间调用相关问题
代码如下:# -*-coding:gb2312 -*-
import json
def get_stored_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 = 'uername.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
return username
def greet_user():
"""问候用户,并指出其名字"""
username = get_stored_username()
if username:
print("Welcom back, " + username.title() + "!")
else:
username = get_new_username()
print("We'll remember you when you come back " + username.title() + "!")
greet_user()
首次运行无异常,按照代码逻辑,在首次创建.json并存储用户名后,第二次运行应该是打印问候语,可是第二次调用的时候还是提示我输入用户名?
哪位大神看看我的问题出在哪里?