import hikari
bot = hikari.GatewayBot(token='OTY1OTU2NTYzNzgzMjc4NTky.Yl6vJw.ESrN-xKdob4Bar80k0sgmS8JjJs')
@bot.listen()
async def ping(event: hikari.GuildMessageCreateEvent) -> None:
if event.is_bot or not event.content:
return
if event.content.startswith('!hi'):
i = 1
if i == 1:
while True:
await event.message.respond('Hello')
await event.message.respond(i)
if i == 1:
await event.message.respond('Hello')
await event.message.respond(i)
else:
break
if event.content.startswith('!stop'):
i = 0
bot.run()我添加了一个while循环语句,这样机器人就可以保持消息传递,直到用户命令它停止为止。但是,即使在添加了一个中断语句之后,它也不会停止。我只想停止while循环而不是注销机器人。是什么导致了这个问题?
发布于 2022-04-19 16:17:51
你想要循环取消如果“!停止”是读对吗?因此,只需让您的代码通过在while循环中包含停止事件就可以这样说。
if event.content.startswith('!hi'):
while True:
await event.message.respond('Hello')
await event.message.respond(i)
if event.content.startswith('!stop'):
break真的想用变量I和数学吗?您需要以不同的结构构造while循环。
while i < 1:
*Do the thing*
if *someone wants it to stop*:
i += 1这里有一个视频链接和有关python循环的指南。这家伙帮助我前进的每一步,当我跌跌撞撞地通过蟒蛇。
https://www.techwithtim.net/tutorials/python-programming/beginner-python-tutorials/while-loops/
如果你喜欢这段录像或指南,我肯定他会有一些不和谐的地方。
https://stackoverflow.com/questions/71927618
复制相似问题