【发布时间】:2021-11-13 18:40:05
【问题描述】:
我有以下代码检查该元素是否在列表中,如果找到该元素,然后它会将其添加到警报中,因此在一个 while 循环中,该元素将显示为 exists 一次。
test_list = [ 1, 6, 3, 5, 3, 4 ]
alert = []
while True:
i = 1
if i in test_list and (i != alert):
print('exists')
alert.append(i)
但它无休止地打印exist。如果在列表中找到该元素,您能否告知我需要做什么,它只打印 1 次为 exists
【问题讨论】:
-
为什么代码中有“while True:”?
-
我在不同的代码上使用了相同的逻辑。
标签: python python-3.x while-loop