import json
filename = 'username.json'
# 如果以前储存了用户,就加载它
try:
with open(filename) as f:
username = json.load(f)
# 如果没有储存过用户,则提示用户输入用户名并存储它
except FileNotFoundError:
username = input("What is your name?")
with open(filename, 'w') as f:
json.dump(username, f)
print(f"We'll remember you when you come back, {username}")
else:
print(f"Welcome back, {username}")
《Python编程:从入门到实践》10.4 储存数据
最新推荐文章于 2024-06-20 16:17:43 发布