for ——else

for stu_dict in student:
    print(stu_dict)
    if stu_dict['name']==find_name:
        print('找到了%s'%find_name)
        break
    else:
        print('抱歉,没有找到%s'%find_name)
print('over')

结果是:

{'name': 'xiao'}
抱歉,没有找到heiq
{'name': 'hei'}
抱歉,没有找到heiq
over

```python
student =[{'name':'xiao'},
          {'name':'hei'}]
find_name ="heiq"
for stu_dict in student:
    print(stu_dict)
    if stu_dict['name']==find_name:
        print('找到了%s'%find_name)
        break
else:
    print('抱歉,没有找到%s'%find_name)
print('over')

结果是:

{'name': 'xiao'}
{'name': 'hei'}
抱歉,没有找到heiq
over

发布了63 篇原创文章 · 获赞 0 · 访问量 1167

猜你喜欢

转载自blog.csdn.net/weixin_2158/article/details/104822537