使用 %s 时出现不支持的操作数类型错误

unsupported operand type(s) error when using %s

我试图让这段代码工作,它将一些 JSON 输出传递到 discord.py 嵌入,但我遇到了一些问题,这是我的代码。

@client.event
async def on_message(message):
    if message.content.startswith('!hello'):
        print("dick")
        embed = discord.Embed(title="Verium Mining", url="https://wiki.vericoin.info/images/thumb/b/b5/Verium-01.png/176px-Verium-01.png", color=0x5da3fd)
        embed.add_field(name="Balance", value='%s coins', inline=False) % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed'])
        embed.add_field(name="Hashrate", value="undefined", inline=False)
        embed.add_field(name="Sharerate", value="undefined", inline=False)
        embed.set_footer(text="undefined")

当我 运行 时出现以下错误:

TypeError: unsupported operand type(s) for %: 'Embed' and 'str'

我该如何解决这个问题?

提前致谢

你在哪里

embed.add_field(name="Balance", value='%s coins', inline=False) % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed'])

你的意思好像是

embed.add_field(name="Balance", value='%s coins' % (json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']), inline=False)

虽然这样写会更清楚:

num_coins = json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']
embed.add_field(name="Balance", value='%s coins'%num_coins, inline=False)

如果您使用的是 Python 的最新版本,则可以使用 f 字符串并完全避免使用 % 运算符:

num_coins = json.loads(requests.get('https://vrm.mining-pool.ovh/index.php?page=api&action=getuserbalance&api_key=90ccec2fe5216ee73f6143ab3f59804d914a0df65165bedddd6f7a78fb6ffd15&id=4483').text)['getuserbalance']['data']['confirmed']
embed.add_field(name="Balance", value=f'{num_coins} coins', inline=False)