使用if语句检查特殊元素!
1.0 检查单个特殊元素:
- names = [‘yushihao’,‘admin’,‘wangshilong’,‘lishiyi’]
- for name in names:
-
if name == 'admin':
-
print('Hello ' + name + ' ,' + 'would you like to see a status report ?')
Hello admin ,would you like to see a status report ?
说明:1.先列出一个列表
2.使用for循环对name进行循环
3.使用if语句对name(我想要的对象为admin)进行选择,若是admin在names中,则进行if语句的缩进代码print()
2.0 检查多个特殊元素:
1.0 names = [‘yushihao’,‘admin’,‘wangshilong’,‘lishiyi’,‘zhouchaohong’,‘xuekunming’,‘wanglifan’] 2.0 namess = [‘admin’,‘xueligao’,‘zhouchaohong’]
3.0 for name in namess:
4.0 if name in names:
5.0 print(‘Hello ’ + name + ’ ,’ + ‘would you like to see a status report ?’)
Hello admin ,would you like to see a status report ?
Hello zhouchaohong ,would you like to see a status report ?
说明:
1.0 为全部人
2.0 为我身边的人
3.0 对namess进行for的循环,让符合条件if的namess中的元素进入4.0的if条件进行判定
5.0 如果namess中的一个或几个元素判定成功,则进行5.0的print()的代码
3.0 使用if语句对列表的是否有无个数进行判断
1.0 names = []
2.0 if names:
3.0 for name in names:
4.0 print(‘Hello ’ + name + ’ ,’ + ‘would you like to see a status report ?’)
5.0 else:
6.0 print(‘no one want to see a status report !’)
>no one want to see a status report !
说明:
1. 用for循环对namess中的元素name进行判断,判断如果为true,则进行if的缩进的代码,就代表names中是有元素的
2. 如果判断结果为false,则进行else后的缩进的代码,则说明names中是无元素的