当site=="IBM"时,输出OK,为什么还要输出site:IBM?

当site=="IBM"时,输出OK,为什么还要输出site:IBM?

相关代码

sites =["Baidu","Google","IBM","Taobao"]

for site in sites:

if site == "IBM":

print("OK")

print("site:" + site)

else:

print("No break")

print("Done!")

运行结果

site:Baidu
site:Google
OK
site:IBM
site:Taobao
No break
Done!


回答:

你又没跳出当前循环 只是在IBM的时候判断语句为真打印了ok
你想要的效果 需要这么写

sites =["Baidu","Google","IBM","Taobao"]

for site in sites:

if site == "IBM":

print("OK")

continue

print("site:" + site)

else:

print("No break")

print("Done!")

以上是 当site=="IBM"时,输出OK,为什么还要输出site:IBM? 的全部内容, 来源链接: utcz.com/p/939063.html

回到顶部