-1
我正在使用事件和數據庫類。使用while循環在列表中添加元素而不覆蓋以前的元素
我創建了一個while循環,它將繼續在我的數據庫中追加我的事件(對象)。 只要用戶沒有輸入命令「exit」,還有一個命令將繼續在if主塊中運行。
我的問題是,無論何時該命令要求添加事件,以前每次迭代都會被擦除。
單詞「事件」的行爲就像命令「退出」所以,每當這個單詞與一個事件字符串一起輸入(我已經實現了一個將事件字符串轉換爲事件對象的函數)。它將繼續向數據庫中添加一個事件對象。
def parse(command):
'''Parse a command string.'''
# gist of event class Event(description, time, date, duration) not part of this function event string could be: '"Movie night" today 10:00pm'
store_event = Database() # where I should save my event objects
cmd_str = command.split()
a_lst =[]
while cmd_str[0] == "event": #while event is a command that the user wants
cmd_str = command.split()
cmd_str.pop(0) # I don't need the word "event" just the event string after it.
new_str = ' '.join(cmd_str)
an_event = parseevent(new_str) # converts string object to event objects
a_lst.append(an_event)
謝謝!
沒有什麼明顯的錯誤與您的代碼(除了在做'cmd_str = command.split()'兩次)。請更清楚地解釋你的輸入是什麼,你得到了什麼結果,以及結果與你的期望有什麼不同。 – 2011-04-06 07:57:11