这个代码就是如果有储存了的用户就加载没有就录入
我录入了并显示we will remember you那段
但是再次运行加载这个文件时发生错误
然后点开这个json文件发现是空的
import json
filename = 'page182.json'
try:
with open(filename) as f:
username = json.load(f)
except FileNotFoundError:
username = input("Your name: ")
with open(filename, 'w') as f:
json.dump = (username, f)
print(f"We will remember you {username}.")
else:
print(f"Hello {username}!")
不知道是哪里出问题了
json.dump = (username, f)
改为:
json.dump(username, f)