我对 discord.py 类型错误有问题:只能将str(而不是"float")连接到str



我在创建一个命令来加载有关比特币价格的信息时遇到了问题。

控制台错误:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: can only concatenate str (not "float") to str

代码如下:

@client.command(pass_context=True)
async def bitcoin(ctx):
url = 'http://api.coinlayer.com/live?access_key='
async with aiohttp.ClientSession() as session:
raw_response = await session.get(url)
response = await raw_response.text()
response = json.loads(response)
await ctx.send("Bitcoin price is: $" + response['rates']['BTC'])

我该如何修复它?

很可能你的response['rates']['BTC']是一个浮点数,因此你不能直接使用+将它连接到字符串

用户str()将其转换为str或使用f-stringsf"Bitcoin price is: $ {response['rates']['BTC']}"

最新更新