python 从入门到实践练习10.12
import json
from pathlib import Path
def get_num(path_str):
context = path_str.read_text()
context = json.loads(context)
return context
def set_num(path):
try:
num = input('请输入你喜欢的数字')
num = int(num)
except ValueError:
print('请输出正确的数据')
else:
num = json.dumps(num)
path.write_text(num)
print('下次打开就知道你喜欢的数字了')
def get_user_num():
"""获取喜欢数字的方法"""
path = Path('ss.txt')
if path.exists():
num = get_num(path)
print(num)
else:
set_num(path)
输出:
1234