for和for循环在python中

时间:2014-11-21 13:57:03

标签: python

这是我第一次在python中尝试和while循环时遇到困难

这是我的代码......

print("Hello and welcome to the programme")

x = 0
y = 0
while (x < 100):
   print ('The count is: ', x + y)
   y = x + y
   x = x + 1

print ("The final answer is:", y)

print("Good Bye")

---------------------------------------

x = 0
y = 1

numberlist = (x + y)


for numbers in numberlist:
    print(int("count is "+ numbers))

我包含while循环的原因是因为我希望for循环与while循环和同一程序完全相同。

提前致谢。

1 个答案:

答案 0 :(得分:2)

使用while循环的for循环的等价物是:

y = 0
for x in range(0, 100):
    print ('The count is: ', x + y)
    y = x + y

如果您只想要最终结果,可以使用:

y = sum(range(0, 100))
相关问题