未定义的变量'json'



我正在用python编写我的第一个Discord bot,如果想打开json文件,我得到以下错误:

命令引发异常:NameError: name 'json' is not defined

脚本部分:

@client.command(aliases=["bal"])
async def balance(ctx):
await open_account(ctx.author)
wallet_amt = users[str(user.id)["wallet"]]
users = await get_bank_data()
em = discord.Embed(title= f"{ctx.author.name}'s balance",color = discord.Color.red())
em.add_field(name = "Balance", Value = wallet_amt)
await ctx.send(embed = em)
async def open_account(user):
users = await get_bank_data()
if str(user.id in users):
return False
else:
users[str(user.id)["wallet"]] = 0

with open("balances.json", "w") as f:
json.dump(users,f)
return True
async def get_bank_data():
with open("balances.json", "r") as f:
users = json.load(f)
return users

json文件

{

}

要解决这个问题,您必须import json.

import json添加到文件顶部。您还没有导入JSON库,因此Python无法识别

这个名称

最新更新