1. After the emergence of json.decoder.JSONDecodeError, Baidu in the process of practicing using JSON, I still can"t find a solution. I hope there is a great god who can help me to answer it.
2. The code is as follows:
import json
def get_stored_num ():
try:
with open("num.json") as f_obj:
num=json.load(f_obj)
except FileNotFoundError:
return None
else:
return num
def get_new_num ():
num=input("tell me your favourite num: ")
with open("num.json","w") as f_obj:
json.dump(num,f_obj)
return num
def num_print ():
num=get_stored_num()
if num:
print("i know your favroite num is: "+ num)
else:
num=get_new_num()
print("your favouite num is: "+num)
num_print ()