我不清楚为什么下面代码的输出是5而不是6:
def counter_gen(size):
cur = 1
while cur <= size:
yield cur
cur = cur + 1
c1 = counter_gen(2)
c2 = counter_gen(2)
Total = 0
for x in c1:
for y in c2:
Total = Total + x + y # Isn't this 0+1+1 in the first iteration and then 2+2+2 in the 2nd iteration, hence giving 6?
print Total
行
Total = Total + x + y
用x==1
和{x==1
和{然后,由于
c2
已经运行了它的过程,内部循环结束。外部循环使用x==2
进行另一次迭代,但是c2
中什么都没有了,因此内部for循环不再被输入相关问题 更多 >
编程相关推荐