繁体   English   中英

Python 印刷基础 1 到 100

[英]Python basics printing 1 to 100

def gukan(count):
    while count!=100:
      print(count)
      count=count+1;
gukan(0)

我的问题是:当我尝试在count=count+1中增加 3 或 9 而不是 1 时,我得到一个无限循环 - 为什么会这样?

这里的答案已经指出,由于递增计数之后,在没有恰好等于100,然后它一直作为条件不满足会(很可能你想<小于100)。

我只想补充一点,您真的应该查看 Python 的内置range函数,该函数从起始值(但不包括)另一个值和可选步骤生成一系列整数 - 因此您可以通过添加 1 或一次 3 或 9 个...

0-100(但不包括100,默认从0开始,以1递增):

for number in range(100):
    print(number)

0-100(但不包括并确保数字不超过 100),分 3 步:

for number in range(0, 100, 3):
    print(number)

当您将count = count + 1更改为count = count + 3count = count + 9count永远不会等于 100。最好的情况是 99。这还不够。

你在这里得到的是无限循环的经典案例: count永远不等于100(当然,在某些时候它会大于 100,但你的while循环不检查这个条件)和while循环继续下去。

你想要的大概是:

while count < 100: # Or <=, if you feel like printing a hundred.

不是:

while count != 0:  # Spaces around !=. Makes Guido van Rossum happy.

现在循环将在count >= 100时终止。

查看 Python文档

第一行定义了变量。 第二行将其循环到 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("")

您的计数永远不会等于值 100,因此您的循环将继续直到为真

将您的 while 子句替换为

def gukan(count):
    while count < 100:
      print(count)
      count=count+3;
gukan(0)

这将解决您的问题,程序在您提供的条件下正确执行。

因为如果你改变你的代码

def gukan(count):
    while count!=100:
      print(count)
      count=count+3;
gukan(0)

计数达到 99,然后在下一次迭代中达到 102。

所以

count != 100

从不评估为真,循环永远继续

如果你想数到 100,你可以使用

def gukan(count):
    while count <= 100:
      print(count)
      count=count+3;
gukan(0)

或(如果你想总是打印 100)

def gukan(count):
    while count <= 100:
      print(count)
      count=count+3;
      if count > 100:
          count = 100
gukan(0)

考虑以下:

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

你应该使用count < 100因为如果你使用 3 或 9 作为增量, count永远不会达到确切的数字 100,从而创建一个无限循环。

祝你好运!~:)

对于这个问题,我们首先要明白range和while循环是什么意思。 这里我们将 i 的值增加 1,

def gukan(count):
    while count!=100:
      print(count)
      count=count+1;
gukan(0)

所以在递增后的某个时间点我们有一个条件

i =100

所以循环中断了。 而当我们使用增量值作为除 5 的倍数以外的任何奇数时,

def gukan(count):
    while count!=100:
      print(count)
      count=count+9;
gukan(0)

我们永远不会遇到 i=100 的情况,因此这会导致无限循环。

输出:

0
9
18
27
36
45
54
63
72
81
90
99
108
117
126 ... till infinite

当您使用count = count + 3 或count = count + 9 而不是count = count + 1 时,count 的值永远不会是100,因此进入无限循环。

您可以使用以下代码使您的条件起作用

而计数 < 100:

现在循环将在计数 >= 100 时终止。

x=1 而 x<=100: 打印(x) x=x+3

我猜它会导致无限循环 bc 你跳过数字 100。如果你将标准设置为小于 101,它应该可以解决问题:)

def gukan(count):
    while count<100:
      print(count)
      count=count+3;
gukan(0)

考虑这个最简单的方法

a = (0)

for i in range(0,100):
     a = a + 1
if a % 3 == 0:
    print(a)
else:
    print("")

因为条件永远不是真的。 即 count !=100 永远不会在您放置 count=count+3 或 count =count+9 时执行。 试试这个.. while count<100

要打印从 1 到 100 的所有数字,只需编写代码:

counter = 1
while counter < 101:
   print(counter)
   counter += 1

暂无
暂无

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

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