我程序中的 while 循环不执行几行代码
The while loop I have in my program doesn't execute a few lines of code
我的 discord.py 机器人程序中的 while 循环没有执行几行代码。我试图让它每次倒计时(也就是发送给用户的消息数)等于限制(也就是我想发送给用户的消息数)时循环中断,但是我注意到机器人即使倒计时达到限制,也会继续发送消息。我绝对没有收到任何错误消息。
据我所知,问题出在下面的代码中。但我不知道它在哪里或是什么:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
while True:
await user.send(f'{content}')
countdown = countdown+1
if countdown == limit:
await ctx.send("The loop has been broken, reason: timeout")
break
else:
continue
使用 for 循环,因为它会 运行 给定的限制次数。
for loop
的代码:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
for i in range(0,limit):
await user.send(f'{content}')
await ctx.send("The loop has been broken, reason: timeout")
如果您想在 while 循环中使用相同的代码,则:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
while countdown <= limit:
await user.send(f'{content}')
countdown = countdown+1
await ctx.send("The loop has been broken, reason: timeout")
我的 discord.py 机器人程序中的 while 循环没有执行几行代码。我试图让它每次倒计时(也就是发送给用户的消息数)等于限制(也就是我想发送给用户的消息数)时循环中断,但是我注意到机器人即使倒计时达到限制,也会继续发送消息。我绝对没有收到任何错误消息。
据我所知,问题出在下面的代码中。但我不知道它在哪里或是什么:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
while True:
await user.send(f'{content}')
countdown = countdown+1
if countdown == limit:
await ctx.send("The loop has been broken, reason: timeout")
break
else:
continue
使用 for 循环,因为它会 运行 给定的限制次数。
for loop
的代码:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
for i in range(0,limit):
await user.send(f'{content}')
await ctx.send("The loop has been broken, reason: timeout")
如果您想在 while 循环中使用相同的代码,则:
@bot.command(pass_context = True)
async def spm_dm(ctx, user : discord.Member):
countdown = 0
await ctx.send(":white_check_mark:")
while countdown <= limit:
await user.send(f'{content}')
countdown = countdown+1
await ctx.send("The loop has been broken, reason: timeout")