这是我第一次在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循环和同一程序完全相同。
提前致谢。
答案 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))