处理循环异常

Siv*_*ram 0 python for-loop exception

a = 6
item_list = [1,2,3,4,5]
for items in item_list:
    if items == a:
        print("match found")
    else:
        print ("no match")
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我希望在完成所有迭代后只打印一次“不匹配”;不是每次迭代。如何修改代码?

ipj*_*ipj 5

改用:

a = 6
item_list = [1,2,3,4,5]
if a in item_list:
    print("match found")
else:
    print ("no match")

Run Code Online (Sandbox Code Playgroud)

无需自己迭代列表,使用内置方法。