Python 基础知识打印 1 到 100
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21184494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python basics printing 1 to 100
提问by user3206308
def gukan(count):
while count!=100:
print(count)
count=count+1;
gukan(0)
My question is: When I try to increment by 3 or 9 instead of 1 in count=count+1I get an infinite loop - why is that?
我的问题是:当我尝试增加 3 或 9 而不是 1 时,count=count+1我得到了一个无限循环 - 为什么会这样?
回答by Nigel Tufnel
When you change count = count + 1to count = count + 3or count = count + 9, countwill never be equal to 100. At the very best it'll be 99. That's not enough.
当您更改count = count + 1为count = count + 3或 时count = count + 9,count永远不会等于 100。最好的情况是 99。这还不够。
What you've got here is the classic case of infinite loop: countis never equalto 100 (sure, at some point it'll be greater than 100, but your whileloop doesn't check for this condition) and the whileloop goes on and on.
你在这里得到的是无限循环的经典情况:count永远不等于100(当然,在某些时候它会大于 100,但你的while循环不会检查这种情况)并且while循环继续并且在。
What you want is probably:
你想要的大概是:
while count < 100: # Or <=, if you feel like printing a hundred.
Not:
不是:
while count != 0: # Spaces around !=. Makes Guido van Rossum happy.
Now the loop will terminate when count >= 100.
现在循环将在 时终止count >= 100。
Take a look at Python documentation.
查看 Python文档。
回答by krystan honour
Your count never equals the value 100 so your loop will continue until that is true
您的计数永远不会等于值 100,因此您的循环将继续直到为真
Replace your while clause with
将您的 while 子句替换为
def gukan(count):
while count < 100:
print(count)
count=count+3;
gukan(0)
and this will fix your problem, the program is executing correctly given the conditions you have given it.
这将解决您的问题,程序在您提供的条件下正确执行。
回答by Paolo
because if you change your code with
因为如果你改变你的代码
def gukan(count):
while count!=100:
print(count)
count=count+3;
gukan(0)
count reaches 99 and then, at the next iteration 102.
计数达到 99,然后在下一次迭代中达到 102。
So
所以
count != 100
never evaluates true and the loop continues forever
从不评估为真,循环永远继续
If you want to count up to 100 you may use
如果你想数到 100,你可以使用
def gukan(count):
while count <= 100:
print(count)
count=count+3;
gukan(0)
or (if you want 100 always printed)
或(如果你想总是打印 100)
def gukan(count):
while count <= 100:
print(count)
count=count+3;
if count > 100:
count = 100
gukan(0)
回答by Jon Clements
The answers here have pointed out that because after incrementing count it doesn't equal exactly100, then it keeps going as the criteria isn't met (it's likely you want <to say less than100).
这里的答案已经指出,由于增加计数后,不等于正好100的话,就一直为准则不满足会(很可能你想要<说的小于100)。
I'll just add that you should really be looking at Python's builtin rangefunction which generates a sequence of integers from a starting value, up to(but not including) another value, and an optional step- so you can adjust from adding 1 or 3 or 9 at a time...
我只想补充一点,您应该真正关注 Python 的内置range函数,该函数从起始值到(但不包括)另一个值和可选步骤生成一系列整数- 因此您可以通过添加 1 或 3 进行调整或一次 9...
0-100 (but not including 100, defaults starting from 0 and stepping by 1):
0-100(但不包括100,默认从0开始,以1递增):
for number in range(100):
print(number)
0-100 (but not including and makes sure number doesn't go above 100) in steps of 3:
0-100(但不包括并确保数字不超过 100),分 3 步:
for number in range(0, 100, 3):
print(number)
回答by Silvia Pratama
consider the following:
考虑以下:
def gukan(count):
while count < 100:
print(count)
count=count+3;
gukan(0) #prints ..., 93, 96, 99
def gukan(count):
while count < 100:
print(count)
count=count+9;
gukan(0) # prints ..., 81, 90, 99
you should use count < 100because countwill never reach the exact number 100 if you use 3 or 9 as the increment, thus creating an infinite loop.
您应该使用,count < 100因为count如果您使用 3 或 9 作为增量,将永远不会达到确切的数字 100,从而创建一个无限循环。
Good luck!~ :)
祝你好运!~:)
回答by Kartik Sharma
Because the condition is never true.
i.e. count !=100 never executes when you put count=count+3 or count =count+9.
try this out..while count<100
因为条件永远不是真的。即 count !=100 永远不会在您放置 count=count+3 或 count =count+9 时执行。试试这个..while count<100
回答by Dijin Joseph
When you use count = count + 3 or count = count + 9 instead of count = count + 1, the value of count will never be 100, and hence it enters an infinite loop.
当您使用count = count + 3 或count = count + 9 而不是count = count + 1 时,count 的值永远不会是100,因此进入无限循环。
You could use the following code for your condition to work
您可以使用以下代码使您的条件起作用
while count < 100:
而计数 < 100:
Now the loop will terminate when count >= 100.
现在循环将在计数 >= 100 时终止。
回答by Josh
The first line defines the variable. The second line loops it to 100, the third adds 1 to a and the 4th divides a by 3 and if there is no remainder (0) it will print that number otherwise it will print a blank line.
第一行定义了变量。第二行将其循环到 100,第三行将 a 加 1,第四行将 a 除以 3,如果没有余数 (0),它将打印该数字,否则将打印一个空行。
a = (0)
for i in range(0,100):
a = a + 1
if a % 3 == 0:
print(a)
else:
print("")
回答by Faizan Khan
x=1 while x<=100: print(x) x=x+3
x=1 而 x<=100: 打印(x) x=x+3

