python aiogram:转换' state' to' int'
假设我想要:
一个机器人询问用户需要多少个甜甜圈,并在响应中收到“甜甜圈”一词乘以用户输入的数字。
简单示例:
bot:您想要多少个甜甜圈?
用户: 5
bot:甜甜圈甜甜圈甜甜圈
在这里我如何看:
class how_many(StatesGroup):
set = State()
async def doughnut(message: types.Message):
await how_many.set.set()
await message.reply('How many doughnuts do you want?')
async def doughnut_answer(message: types.Message, state: FSMContext):
await message.reply("doughnut" * int(how_many.set))
await State.finish()
def handlers(dp: Dispatcher):
dp.register_message_handler(doughnut, commands='doughnuts', state=None)
dp.register_message_handler(doughnut_answer, state=how_many.set)
,但我遇到了一个错误: “ int()参数必须是字符串,类似字节的对象或一个真实数字,而不是'状态'“
我如何”状态“转换为“ int”? 为此,我需要在某个地方保存值然后将其转换?还是可以使用正确的语法直接执行此操作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
州班的样子是什么样的?我假设您将值存储在状态对象中。此外,必须实现
__ INT __
方法,否则int
函数不能应用于状态对象。使用此方法,您可以返回存储的值(在下面的示例中,我称之为此值数量):int
函数仅调用类的__ INT __
类的方法。由于您的how_many.set
是状态对象,因此int
函数返回存储的值数量,以获取有关Python魔法方法的更多信息,请参见在这里
What does the State class look like? I assume you store the value in the State object. Furthermore, the
__int__
method must be implemented, otherwise theint
function cannot be applied to a State object. With this method you can return the stored value (in my example below I have called this value amount):The
int
function only calls the__int__
method of a class. Since yourhow_many.set
is a State object, theint
function returns the stored value amountFor more information regarding Python Magic Methods see here