[英]While loop inside for loop?
I have this code as an example: 我以此代码为例:
for i in func(x):
count = 1
while i > 0:
if count % 2 == 0:
print("(:")
count += 2
else:
print("(:")
count += 2
Or this one: 或者这一个:
for i in func(x):
count = 0
if count % 2 == 0:
print("(:")
count += 1
else:
print(":(")
count += 1
They don't work as I want them to work. 它们不起作用,因为我希望它们起作用。
The first one just enters in an infinite loop, the second one only prints the happy smiley I want it to print the happy smiley then the sad smiley an so on 第一个只是进入一个无限循环,第二个只打印出幸福的笑脸我希望它打印出幸福的笑脸然后是悲伤的笑脸等等
It looks like you're trying to use i
and count
as two different values to use while iterating, and I don't think you need to do this to get what you want. 看起来你正在尝试使用
i
并count
迭代时使用的两个不同的值,我认为你不需要这样做才能得到你想要的东西。
First of all - a while
statement is basically a loop that runs until it is False. 首先 -
while
语句基本上是一个循环,直到它为False。 So if you have 所以,如果你有
green = True
while green:
print("Hello")
...this will never end. ......这将永远不会结束。 Placing the
while
in a for
loop doesn't change that; 将
while
放在for
循环中不会改变它; it simply waits for the while my_condition
to turn False. 它只是等待
while my_condition
变为False。
Additionally, in your first loop you are incrementing count
-- but the while
loop is looking at the value of i
, which never gets a chance to change because the while
never becomes untrue! 另外,在你的第一个循环中你正在递增
count
- 但是while
循环正在查看i
的值,它永远不会有机会改变,因为while
永远不会变得不真实!
Finally - in your second loop, you're setting count
at the beginning of each iteration! 最后 - 在你的第二个循环中,你在每次迭代开始时设置
count
! This means that count
will never actually progress in a meaningful way; 这意味着
count
永远不会以有意义的方式实际发展; you set it to 0, print an emoticon, increment it by 2, then continue in the for
loop - resetting count
to 0! 将其设置为0,打印表情符号,将其递增2,然后继续执行
for
循环 - 将count
重置为0!
Basically, just realize that you can use i
directly, there's no need for a count
variable as well. 基本上,只要意识到你可以直接使用
i
,也不需要count
变量。 for i in something
will iterate over whatever is in something
if it is an iterable thing. 如果它是一个可迭代的东西,
for i in something
会迭代something
东西。 So for example: 例如:
for i in range(0, 10):
if i % 2 == 0:
print(":)")
else:
print(":(")
EDIT: After seeing the original code, it looks like you are trying to produce a generator object. 编辑:看到原始代码后,看起来你正在尝试生成一个生成器对象。
Using yield
in conjunction with while
or for
will net you a generator - an object you can iterate over. 将
yield
与while
或for
结合使用将为您生成一个生成器 - 您可以迭代的对象。 Here's a baseline example of that. 这是一个基线的例子。
>>> def baz(x):
... i = 0
... while i > x:
... yield i
... i += 1
>>> baz(4)
<generator object baz at 0x1004935a0>
>>> me = baz(4)
>>> me.next()
0
>>> me.next()
1
>>> me.next()
2
>>> me.next()
3
>>> me.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
In a situation where you just want to generate a set number of objects, you can skip the while
and just write a for
, which will save you a couple of lines. 在您只想生成一定数量的对象的情况下,您可以跳过
while
而只是写一个for
,这将为您节省几行。 For example, the following code does the same as the above: 例如,以下代码与上面的代码相同:
def foo(x):
for i in range(0, x):
yield i
However, in the example you've shown me there seems to be some more involved math during the while
loop, so it does make sense to use a while
sometimes. 但是,在你向我展示的例子中,在
while
循环期间似乎有一些更复杂的数学运算,所以有时使用一段while
是有意义的。 Imagine a situation where you wanted to exit the loop when i
became prime, for example… 想象一下,当
i
成为素数时你想要退出循环的情况,例如......
def until_prime(x):
while is_not_prime(x): ## or, 'while not is_prime(x)', whatever our imaginary function does
x = x * (x + 1)
yield x
I have no idea if the code above will ever generate a prime number, so please don't judge it too harshly. 我不知道上面的代码是否会生成素数,所以请不要过于严厉地判断它。 :D Nevertheless, it is an example of a situation where you couldn't necessarily write a
for
loop, so you'd need to wait until a condition arose. :D然而,这是一个你无法编写
for
循环的情况的例子,因此你需要等到条件出现。
You probably want the enumerate()
function. 您可能需要
enumerate()
函数。 It appears that i
is never used, but that is actually irrelevant. 似乎
i
从未使用过,但这实际上是无关紧要的。
The enumerate function takes an iterable (so anything you can loop over) and returns a tuple with the count and each item in order. 枚举函数采用可迭代(所以你可以循环的任何东西)并返回一个带有计数和每个项目的元组。
for index,item in func(x):
if index % 2 == 0:
print("(:")
else:
print(":(")
This way, even if you don't use i
(or in the above case item
) yet , you have the option of doing so in the future. 这样一来,即使你不使用
i
(或者在上述情况下item
), 然而 ,你对未来有这样的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.