Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord.py API:当用户输入时循环不会中断;宾果游戏“;_Python_Discord_Discord.py - Fatal编程技术网

Python Discord.py API:当用户输入时循环不会中断;宾果游戏“;

Python Discord.py API:当用户输入时循环不会中断;宾果游戏“;,python,discord,discord.py,Python,Discord,Discord.py,当您输入bingo时,函数应该中断,但由于某些原因,userinput字符串没有任何值。它是在代码前面定义的,因此它不是问题的原因 请帮忙,谢谢 elif "!bingo" in message.content.lower(): await message.channel.send("Here's your miniature Bingo sequence") shuffled = shuffle(bingo)

当您输入bingo时,函数应该中断,但由于某些原因,userinput字符串没有任何值。它是在代码前面定义的,因此它不是问题的原因

请帮忙,谢谢

elif "!bingo" in message.content.lower():
        await message.channel.send("Here's your miniature Bingo sequence")
        shuffled = shuffle(bingo)
        await message.channel.send(bingo[0:3])
        await message.channel.send(bingo[3:6])
        await message.channel.send(bingo[6:9])
        channel = message.channel
        while userinput != "bingo":
            await message.channel.send("Type anything to move on, or type bingo to end!")
            def check(m):
                return m.channel == channel
                userinput[0](str(m.content))
            if message.author == client.user:
                return
            else:
                msg = await client.wait_for('message', check=check)
                if str(userinput) == "bingo":
                    break
                else:
                    await message.channel.send(random.randint(1,9))
第二部分永远不会执行,因为您总是返回第一条语句。 因此,
userinput
从未按预期设置为
m.content

非常简单的答案:

    def check(m):
        return m.channel == channel
        userinput[0](str(m.content))
break
用于退出函数。不管怎么说,它在设计工作中并没有那么好地发挥作用


如果我错了,很抱歉,我对discord.py非常陌生,但我对Python并不陌生。Discord.py可能正在更改与Python有关的内容。如果这不起作用,很遗憾,我可能无法帮助您。

我认为中断用于中断while循环,而不是函数。在这一点上使用返回会改变行为,因为在这个函数的while循环之后不会执行任何代码。正如我前面所说,
break
不是很可靠。你怎么会这样认为?我从来没有听说过任何情况下,休息不工作,因为它应该。你有什么证据或例子吗?关于break应该做什么,请参阅Python:“break语句,就像在C中一样,脱离了最内部的for或while循环。”根据我的经验,
break
无法退出程序。退出程序是什么意思?既不返回也不中断退出程序。Return退出函数,break退出循环或开关/案例…谢谢大家,我找到了解决方案。这两条注释并没有完全解决问题(尽管第二条注释帮助我找到了存在的问题),循环很好,但问题是您无法将
str(userinput)
与字符串进行比较,因为
userinput
变量是一个列表。一个非常业余的错误。无论如何,if语句被修改为userinput:中的if“bingo”。其他一切都保持不变。再次感谢你的帮助!你可以自己回答并接受你的问题,这有助于其他人在将来找到这个答案。谢谢你的帮助!此语句中存在问题,因为它没有执行检查。然而,还有一个更明显的问题,
userinput
无法进行比较。我在语句中使用了一个
if str(userinput) == "bingo":
    return