2016-11-16 78 views
-6

我最近開始使用Python 3.5.2編程(我大約三年前學過C++,但從那時起我還沒有使用它),我無法理解'.append()'何時使用append?

也許問題是我不是英語母語的人。

有人可以向我解釋這個概念嗎?

編輯:謝謝。我不能讓這個代碼工作。基本上,我希望用戶輸入日,月,年並將它們保存到GDO中。我的錯誤是什麼?

from tkinter import * 


root = Tk() 
root.title("Calendar") 
root.geometry("300x300") 

GDO1 = ['Day', 'Month', 'Year'] 
GDO = [] 
for w in range (3): 

    en = Entry(root) 
    lab = Label(root, text = GDO1[w]) 
    lab.grid(row=w+1, column=0, sticky = W) 
    en.grid(row=w+1, column=1, sticky = W) 
    GDO.append(en) 

buttonGDO = Button (root, text="Submit", command=GDO.append(en) and print (GDO)) 
buttonGDO.grid(row=4) 


root.mainloop 
+5

這真的看起來一個典型的案例RTFM ... –

回答

3

你有例如列表[1,2,3] 如果要添加其他元素使用追加:

list = [1, 2, 3] 
list.append(4) 
2

追加功能追加對象到現有列表。

參見文檔:list.append

編輯: 在你的具體的例子,這個問題是不是追加。 mainloop是一個函數調用,所以你需要調用它這樣,用括號:

root.mainloop()

2

追加是非常簡單的,它只是增加了,或者追加,一個值的列表。

>>> list = ['one', 'two', 'three'] 
>>> list 
['one', 'two', 'three'] 
>>> list.append('four') 
>>> list 
['one', 'two', 'three', 'four'] 
5
consider if you have List = [1,2,3,4] 
#append function - Adds an item to the end of the list. 
>>>L = [1,2,3,4] 
>>>L.append(5) 
>>>print(L) 
>>>[1,2,3,4,5]