weixin_43812695 2021-01-07 12:29 采纳率: 100%
浏览 383
已采纳

json.dump 没报错但打开json文件没写进去

import json
filename = 'user_fav_num.json'

def get_num():
    num = input("What's your favrotite number? ")
    print("We will remember it next time you visit!")
    return str(num)

try:
    with open(filename) as f:
        user_fav_num = json.load(f)
except FileNotFoundError:
    user_fav_num = get_num()
    with open(filename, 'w') as f:
        json.dump = (user_fav_num, f)
else:
    print(f"Your favorite number is {user_fav_num}, isnt it?")
  • 写回答

1条回答 默认 最新

  • pathletboy 2021-01-07 13:11
    关注

    把15行的 = 号去掉即可。

    import json
    filename = 'user_fav_num.json'
     
    def get_num():
        num = input("What's your favrotite number? ")
        print("We will remember it next time you visit!")
        return str(num)
     
    try:
        with open(filename) as f:
            user_fav_num = json.load(f)
    except FileNotFoundError:
        user_fav_num = get_num()
        with open(filename, 'w') as f:
            json.dump(user_fav_num, f)
    else:
        print(f"Your favorite number is {user_fav_num}, isnt it?")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ucinet怎么计算子群体个数和核心边缘结构拟合度
  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度