我希望我的代码在用户一一键入新元素时不断向我的列表中添加新元素。但是代码没有这样做

I want my code to keep adding new elements to my list as the user types them one by one. But the code does not do it

这是我的代码。我希望它在用户键入元素时不断向列表中添加元素。

**while True:
    _list = []
    new_element = input('typehere:')
    _list.append(new_element)
    print(_list)**

这是输出:

typehere:**element_1**
['element_1']
typehere:**element_2**
['element_2']

我要:

typehere:element_1
['element_1']
typehere:element_2
[ 'element_1' , 'element_2' ]

在循环外初始化list

_list = []

while True:
    new_element = input('typehere:')
    _list.append(new_element)
    print(_list)

输出:

typehere:>? element_1
['element_1']
typehere:>? element_2
['element_1', 'element_2']

哦。没关系……我现在明白了……每次循环重新启动时,变量都不会保存以前的值,因为我已将其声明为空列表。此代码有效:

liist = []
while True:
    new_element = input('Type here :')
    liist.append(new_element)
    print(liist)