Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 奇怪的行为_Python_Python 3.x - Fatal编程技术网

Python 奇怪的行为

Python 奇怪的行为,python,python-3.x,Python,Python 3.x,可能重复: 鉴于以下情况,如何正确实现列表理解、退出循环并在“else”之后运行语句?我曾尝试扫描“-”字符,但它不起作用 已尝试: while(current != randomValue) 正在尝试: randomKey = random.choice(list(topic.keys())) randomValue = random.choice(topic[randomKey]) current = "-" * len(randomValue) while (i for i

可能重复:

鉴于以下情况,如何正确实现列表理解、退出循环并在“else”之后运行语句?我曾尝试扫描“-”字符,但它不起作用

已尝试:

while(current != randomValue)
正在尝试:

randomKey = random.choice(list(topic.keys()))

randomValue = random.choice(topic[randomKey])

current = "-" * len(randomValue)  

while (i for i in range (0, len(current)) if i != "-"):
    (statements)
else:
    (statements)

您的测试没有意义——生成器上的布尔值总是
True

>>> a = (i for i in range(10))
>>> bool(a)
True

另外,
i
将永远不会=='
-'
,因为您正在
范围内循环。我不确定您在尝试什么,但我们确实无法帮助您解决这个问题,除非我们看到您能够将实际代码缩减到最重要的部分。(谁知道,通过这样做,你自己可能会发现问题)

而不是列表理解,努力使while语句错误等等。。。我这样做,在某种程度上起了作用:

while ("-" in current ):

嗨:)我尝试了一个变通方法,因为我非常需要我的上一篇文章。同样的问题和人顺便说一下:)对不起,非常感谢我尝试了别的东西&它奏效了。这么长时间后,一个简单的解决方案。我真蠢。非常感谢我尝试了别的东西&它奏效了。这么长时间后,一个简单的解决方案。愚蠢的我。