如何在遍历列表时,没有出现结果,只提示一次?

如何在遍历列表时,没有出现结果,只提示一次?

问题描述:

如何在遍历列表时,没有出现结果,只提示一次?代码如下,即使只提示一次Bad

代码如下:
list1 = [1,2,3,4,5,6,7,8,9,10]
a=11
for item in list1:
if a == item:
print("Great")
else:
print("Bad")

输出结果是:
Bad
Bad
Bad
Bad
Bad
Bad
Bad
Bad
Bad
Bad

有not in 为啥你放着不用还要循环一遍

if a  not in list1:
    print('Bad')
else:
    print('Great')

这就是一种结果啊,list里面没有值为11的元素,循环打印自然显示的是10次Bad打印结果

增加一个标志变量:
flag=False
for item in list1:
if item==a:
print('Great')
elif not flag:
print('Bad')
flag=True
else:
pass

print("Great" if a in list1 else "Bad")

list1 = [1,2,3,4,5,6,7,8,9,10]
a=11
logo=true
for item in list1:
if a == item:
print("Great")
else:
if(logo){
print("Bad")
logo=false
}

list1 = [1,2,3,4,5,6,7,8,9,10]
a=11
if a in list1:
print('Great')
else:
print('bad')