2017-06-01 80 views
-1
def get(i, x): 
i = 0 
numbers = [] 
while i < x: 
    print ('At the top i is %d' % (i)) 
    numbers.append(i) 

    i = i + 1 
    print ("Numbers now: "), numbers 
    print ("At the bottom i is %d" % (i)) 

print ("The numbers: ") 

for num in numbers: 
    print (num) 
get(1,6) 

所以,當我調用該函數是這樣的結果:列表追加不工作

At the top i is 0 
Numbers now: 
At the bottom i is 1 
At the top i is 1 
Numbers now: 
At the bottom i is 2 
At the top i is 2 
Numbers now: 
At the bottom i is 3 
At the top i is 3 
Numbers now: 
At the bottom i is 4 
At the top i is 4 
Numbers now: 
At the bottom i is 5 
At the top i is 5 
Numbers now: 
At the bottom i is 6 
The numbers: 
0 
1 
2 
3 
4 
5 

那麼,爲什麼"Numbers now: "沒有顯示什麼? print聲明,append函數還是有什麼問題?我迷路了,感謝任何幫助。

+1

因爲它應該是右括號內) –

+0

感謝的人,順便說一下,爲什麼我一直都是downvoted呢? – Dan

+0

我不是downvoter,但也許這是因爲在Python 3.x中'print'是一個**函數**,而不是一個語句,因此只能輸出它作爲參數傳遞的東西......或者是因爲它好像你應該能夠自己解決這個問題。 – martineau

回答

2

您需要在打印語句的括號內包含numbers

print ("Numbers now: ", numbers),而不是print ("Numbers now: "), numbers