如何使用 while 循环打印 Python 范围内的每个数字
How to use a while loop to print every number in a range in Python
我正在研究一个练习题,它说:“创建一个函数,它接收一个整数作为参数(步长)并打印从 0 到 100(包括在内)的数字,但在每个数字之间留下步长。
两个版本:for 循环和 while 循环。
我可以使用 foor 循环来完成:
def function(x):
count = 0
for x in range(0, 100, x):
print(x)
我似乎无法让它与 while 循环一起工作。我试过这个:
def function(x):
count = 0
while count <= 100:
count += x
print(count)
所以请帮忙。谢谢!
您需要在打印后递增count
。
def function(x):
count = 1
while count <= 100:
print(count)
count += x
我正在研究一个练习题,它说:“创建一个函数,它接收一个整数作为参数(步长)并打印从 0 到 100(包括在内)的数字,但在每个数字之间留下步长。 两个版本:for 循环和 while 循环。
我可以使用 foor 循环来完成:
def function(x):
count = 0
for x in range(0, 100, x):
print(x)
我似乎无法让它与 while 循环一起工作。我试过这个:
def function(x):
count = 0
while count <= 100:
count += x
print(count)
所以请帮忙。谢谢!
您需要在打印后递增count
。
def function(x):
count = 1
while count <= 100:
print(count)
count += x