为了账号安全,请及时绑定邮箱和手机立即绑定

当我尝试向用户添加 exp 时,出现 KeyError: '563690668808208423'

当我尝试向用户添加 exp 时,出现 KeyError: '563690668808208423'

慕的地10843 2023-04-18 15:50:52
我收到 KeyError: '563690668808208423',我正在尝试在用户使用命令时向用户添加 exp@bot.command(pass_context=True)async def work(ctx):    with open('data.json','r+', encoding = 'utf-8') as f:        kingdom = json.load(f)    if str(kingdom[str(ctx.author.id)]['job']) == "Cleaner":        await add_experience(kingdom, ctx.author ,10)        await ctx.send('Вы отлично поработали')async def add_experience(users, user, exp):    users[user.id]['experience'] += exp我使用 json 文件作为数据库:{"563690668808208423": {"experience": 0, "level": 1, "job": "Cleaner"},"517371726720532511": {"experience": 0, "level": 1, "job": "Cleaner"}}563690668808208423是我的ID
查看完整描述

1 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

您应该将其更改为以下内容:

async def add_experience(users, user, exp):
    users[str(user.id)]['experience'] += exp

从用户返回的 id 即用户 id以 int(整数)形式返回,因此您必须先将其更改为字符串


查看完整回答
反对 回复 2023-04-18
  • 1 回答
  • 0 关注
  • 69 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信