Не получается делать математические решения
Делаю бота для рп. Нужно чтобы отнимались числа.
@client.command()
async def тест_создать(ctx):
def check(m: discord.Message): # m = discord.Message.
return m.author.id == ctx.author.id and m.channel.id == ctx.channel.id
await ctx.send('Введите кол-во дерева')
msg = await client.wait_for('message', check=check)
cost = str(3)
answer = str(msg.content)
if answer < cost:
await ctx.send('Недостаточно ресов')
if answer >= cost:
await ctx.send('Вы скрафтили челов')
await ctx.send({answer}-{cost} + ' дерева осталось')
В итоге ошибка выглядит так:
Ignoring exception in command тест_создать:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\zombibox\Desktop\Питон проэкты\попыт рп\politrp.py", line 75, in тест_создать
await ctx.send({answer}-{cost} + ' дерева осталось')
TypeError: unsupported operand type(s) for +: 'set' and 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: unsupported operand type(s) for +: 'set' and 'str'
Ответы (2 шт):
Автор решения: RAINGM
→ Ссылка
Из ошибки ясно, что нельзя складывать set и str
@client.command()
async def тест_создать(ctx):
def check(m: discord.Message): # m = discord.Message.
return m.author.id == ctx.author.id and m.channel.id == ctx.channel.id
await ctx.send('Введите кол-во дерева')
msg = await client.wait_for('message', check=check)
cost = str(3)
answer = str(msg.content)
if answer < cost:
await ctx.send('Недостаточно ресов')
if answer >= cost:
await ctx.send('Вы скрафтили челов')
await ctx.send({int(answer)}-{int(cost)} + ' дерева осталось')
Автор решения: CrazyElf
→ Ссылка
Наверное так нужно:
cost = 3
answer = int(msg.content))
if answer < cost:
await ctx.send('Недостаточно ресов')
if answer >= cost:
await ctx.send('Вы скрафтили челов')
await ctx.send(f'{answer-cost} дерева осталось')
То есть:
- сравниваем числа, а не строки
- генерируем выходную строку с помощью
f-string
А у вас фигурные скобки использовались без f-string, из-за чего воспринимались, как оборачивание переменной в множество.