虽然不在列表中

Reg*_*ald 2 python while-loop python-3.x

我正在尝试编写一个代码,我想问你怎么能问一个while循环重复,直到它找到一个单词,例如在我的情况下在列表中的END.

L=[]
while "END" (not) in L :
   L=L.append(something)
Run Code Online (Sandbox Code Playgroud)

问你,如果你不明白我的意思.

Woo*_*ble 8

您编写的代码将正常工作,除非list.append()返回None,并修改列表; 不要将它分配给L:

L=[]
while "END" not in L :
   L.append(something)
Run Code Online (Sandbox Code Playgroud)


Don*_*sto 7

>>> a = [1,2,3]
>>> while 12 not in a:
...  a.append(len(a)+1)
...
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Run Code Online (Sandbox Code Playgroud)

例如 ...