2014-11-21 75 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