Python 使用while循环在列表中添加元素而不覆盖以前的元素

Python 使用while循环在列表中添加元素而不覆盖以前的元素,python,Python,我正在处理事件和数据库类 我创建了一个while循环,它将继续在数据库中附加我的事件(对象)。 还有一个命令,只要用户不键入命令“exit”,它就会在if主块中继续运行 我的问题是,每当命令要求添加事件时,每次迭代都会删除上一个事件 “event”一词的作用类似于命令“exit”,因此每当这个词与事件字符串一起键入时(我已经实现了一个将事件字符串转换为事件对象的函数)。它将继续向数据库中添加事件对象 def parse(command): '''Parse a command strin

我正在处理事件和数据库类

我创建了一个while循环,它将继续在数据库中附加我的事件(对象)。 还有一个命令,只要用户不键入命令“exit”,它就会在if主块中继续运行

我的问题是,每当命令要求添加事件时,每次迭代都会删除上一个事件

“event”一词的作用类似于命令“exit”,因此每当这个词与事件字符串一起键入时(我已经实现了一个将事件字符串转换为事件对象的函数)。它将继续向数据库中添加事件对象

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)

谢谢

我想你应该这样做:

def parse(command, a_lst):
    '''Parse a command string.'''
    a,b = command.split(None,1)
    if a == "event":
        a_lst.append(b)


store_event = Database() # where I should save my event objects
parse('event blahblahbla',store_event)
parse('event youtchouiya',store_event)
parse('event print ramantiyi',store_event)
parse('event import re',store_event)
# etc etc 

代码没有明显的错误(除了执行两次
cmd\u str=command.split()
)。请更清楚地解释您的输入是什么,您得到了什么结果,以及该结果与您期望的结果有何不同。