简体   繁体   English

在这三种情况下,计数的结果有何不同?

[英]How are the results for count different in all these three cases?

Code 1:代码 1:

iteration = 0

count = 0

while iteration < 5:

    for letter in "hello, world":

        count += 1

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1

Code 2:代码 2:

iteration = 0

while iteration < 5:

    count = 0

    for letter in "hello, world":

        count += 1

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1

Code 3:代码 3:

iteration = 0

while iteration < 5:

    count = 0

    for letter in "hello, world":

        count += 1

        if iteration % 2 == 0:

            break

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1

For Code 1 , you're continuing to add on to the count.对于Code 1 ,您将继续添加计数。 So during the first iteration, the count becomes 12 (the length of "hello, world" is 12), and then during the second iteration, you never reset the count to 0 so count is continuously added on until it reaches 24, as it adds on the length of "hello, world" again (12 + 12 = 24).因此,在第一次迭代期间,计数变为 12(“hello, world”的长度为 12),然后在第二次迭代期间,您永远不会将计数重置为 0,因此计数会不断增加,直到达到 24,因为它再次增加“hello, world”的长度(12 + 12 = 24)。

0 + len("hello, world") = 12

12 + len("hello, world") = 24

and so forth

For Code 2 , count is reset to 0 each time.对于代码 2 ,计数每次都重置为 0。 This means that the count will always equal 12, as the length of "hello, world" is 12.这意味着计数将始终等于 12,因为“hello, world”的长度是 12。

0 + len("hello, world") = 12

reset to 0 

0 + len("hello, world") = 12

and so forth

For Code 3 , you break every time the iteration is an even number.对于Code 3 ,每次迭代为偶数时都会中断。 That means for iterations 0, 2, and 4, iteration returns a value of 1 as 1 is added to iteration at the beginning of the for loop.这意味着对于迭代 0、2 和 4,迭代返回值 1,因为在 for 循环开始时将 1 添加到迭代中。 But during odd iterations, the count is 12, since the program does not break out of the for loop and adds the length of "hello, world", which is 12.但是在奇数迭代期间,计数为 12,因为程序没有跳出 for 循环并添加“hello, world”的长度,即 12。

count = 0

For loop
     "h" in "hello, world"

     Add 1, 0 -> Is it even or odd? Even, Do not add len("ello, world") to count

1

count = 0

For loop
     "h" in "hello, world"

     Add 1, 0 -> Is it even or odd? Odd, Do add len("ello, world") to count

12

Code 1:代码 1:

Your count is set outside the while loop, therefore it is not effected by it and thus increase by 12 len(hello, world) = 12您的计数设置在while循环之外,因此不受它影响,因此增加 12 len(hello, world) = 12

Code 2:代码 2:

Your count is inside the while loop, therefore it resets with each iteration ONLY.您的计数在while循环内,因此它仅在每次迭代时重置。 That's why count = 12 is staying the same while Iteration , being outside the loop, increases.这就是为什么count = 12保持不变而Iteration在循环外增加的原因。

Code 3:代码 3:

When your Iteration is even the code is breaking after the first count.当您的Iteration甚至在第一次计数后代码就会中断。 When it's odd it runs through the code fine.当它很奇怪时,它可以很好地运行代码。

In code 1 during every iteration, you are adding value of count to the previous one.在每次迭代期间的代码 1 中,您将count添加到前一个值。 While in the code 2, at the beginning of every iteration, you are re-assigning the count = 0 .而在代码 2 中,在每次迭代开始时,您都会重新分配count = 0 In each iteration, after the for loop is executed, 12 is added to the previous value of count which in case of code 2 is always 0 .在每次迭代中,在执行 for 循环后,将 12 添加到计数的前一个值,在代码 2 的情况下始终为0 Hence value of count is different in both the case.因此,这两种情况下计数的值是不同的。

In case 3, after the execution of for loop, either you count value will be 1 (if the value of iteration is even) or 12 (if the value of iteration is odd).在情况 3 中,执行 for 循环后,您的计数值将是 1(如果迭代的值为偶数)或 12(如果迭代的值为奇数)。 This is because of the if condition that check for the i%2.这是因为检查 i%2 的 if 条件。 Hence in case 3, value of count is different for odd and even.因此,在情况 3 中,奇数和偶数的计数值不同。 And since during each iteration you are reassigning count = 0 , value of count for all odd number is 12 and value of count for all even number is 1.并且由于在每次迭代期间您都在重新分配count = 0 ,因此所有奇数的计数值为 12,所有偶数的计数值为 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM