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

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 根据会员信息表绘制堆积柱状图
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统
  • ¥100 rtmpose姿态评估
  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,