2014-11-21 79 views
-4

循环这是我第一次尝试了,虽然在蟒蛇循环,我陷入对于和虽然蟒蛇

这里是我的代码...

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)) 

我列入,而原因循环是因为我希望for循环的工作方式与while循环和同一个程序完全相同。

在此先感谢。

+4

我最好给你的建议是去并做一些python初学者教程。例如,在codeacademy.org上有一个,在udacity.com上有一对是初学者 – Totem 2014-11-21 13:59:51

回答

2

相当于使用for循环您while循环是:

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

如果你只是想最后的结果,你可以使用:

y = sum(range(0, 100)) 
+0

非常感谢丹尼尔,这是一个很好的帮助:) – CFC 2014-11-21 14:01:31