Python while循环,打印连续的奇数数字。

huangapple go评论85阅读模式
英文:

Python while loop, print sequential odd numbers

问题

Line 9 (count = 0) is needed to reset the count of sequential odd numbers encountered to zero. Line 2 (count = 0) initializes the count variable, but without line 9, the count would keep increasing without ever resetting, and the loop would not work as intended. This is because line 7 (if count == 3) checks if three sequential odd numbers have been encountered, and when this condition is met, it prints the current odd number and resets the count to 0, allowing the loop to continue correctly.

英文:

I'd appreciate some clarification on my python code.

The goal was to create a while loop in order to print every third sequential odd number between 2 and 19, inclusive. (The expected output is 7, 13, 19.)

After some trial and error, I got the following code to work.

i = 2
count = 0
while i <= 19:
    if (i%2 != 0):
        count += 1    
      
        if count == 3:
            print(i)
            count = 0
          
    i += 1

However, this is what I incorrectly expected to work:

i = 2
count = 0
while i <= 19:
    if (i%2 != 0):
        count += 1    
      
        if count == 3:
            print(i)
           
    i += 1  

Why is line 9 (count = 0) needed in order for this to run properly? Why isn't line 2 enough?

答案1

得分: 0

以下是翻译的内容:

尝试跟踪代码并查看循环的每次迭代中变量 icount 的值。

例如,你的第一个代码片段将产生以下结果:

i=2, count=0  # 初始化
i=3, count=1
i=4, count=1
i=5, count=2
i=6, count=2
i=7, count=3  # 打印 7,count 重置为 0
i=8, count=0
i=9, count=1
...

而第二个代码片段将产生以下结果:

i=2, count=0  # 初始化
i=3, count=1
i=4, count=1
i=5, count=2
i=6, count=2
i=7, count=3  # 打印 7 后,count 不会重置
i=8, count=3
i=9, count=4
...

请注意,在第二个示例中,count 在打印第一个值后始终大于或等于 3,因为它从未减少或重置为 0。你可以修改该行为 if count % 3 == 0,并且在打印后不重置 count 为 0。

英文:

Try walking through the code and tracking what the variables i and count are at each iteration of the loop.

For example your first snippet will produce:

i=2, count=0  # init
i=3, count=1
i=4, count=1
i=5, count=2
i=6, count=2
i=7, count=3  # 7 is printed, count is reset to 0
i=8, count=0
i=9, count=1
...

While the second will produce:

i=2, count=0  # init
i=3, count=1
i=4, count=1
i=5, count=2
i=6, count=2
i=7, count=3  # 7 is printed, count is not reset
i=8, count=3
i=9, count=4
...

Notice in the second example that the count is always greater than or equal to 3 after printing the first value because it's never reduced or set back to 0. You could modify that line to if count % 3 == 0 and not reset the count to 0 after printing as well.

答案2

得分: 0

以下是翻译好的部分:

这是因为您在整个循环中指定的条件是仅当计数等于3时打印奇数的值。您的代码之所以不起作用,是因为当涉及到下一个3个奇数时,您的计数将增加到6,但仅在等于3时才满足条件。因此,解决这个问题的更好方法是设置一个更好的条件来检查是否已计算了每3个元素。您应该尝试:

# 在计数到3个奇数时打印
if (count % 3 == 0):
      print(i)

这比每次循环都将计数设置为0要好。只需提出新的条件。

英文:

This is because the condition that you specify in the whole loop is to print the value of that odd when your count is equal to 3 only. The reason why your code did not work is that when it comes to the next 3 odd numbers, your count will be added up to be 6, but your condition will met when it equal to 3 only. Therefore, the better way to fix this problem is to set a better condition to check for every 3 elements are counted. You should try :

# print when 3 odds are counted
if (count % 3 == 0):
      print(i)

This is better than always set count to be 0 at every loop. Just come up with the new condition

huangapple
  • 本文由 发表于 2023年4月17日 02:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定