2017-06-01 72 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