簡體   English   中英

列表附加無效

[英]List appending is not working

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函數或什么有問題嗎? 我在這里迷路了,感謝您的幫助。

您需要在print語句中的括號內包括numbers

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM