day04-if判断和for、while循环
一 、 if条件判断的几种常见形式:
(1):如果条件成立,挨个执行代码1、2、3
if 条件:
代码1
代码2
代码3
cls='human'
sex='female'
age=18
if cls == 'human' and sex == 'female' and age > 16 and age < 22:
print('开始表白')
print('end....')
(2):if和else使用 当if条件不成立执行else中代码
if 条件:
代码1
代码2
代码3
else:
代码1
代码2
代码3
cls='human'
sex='female'
age=38
if cls == 'human' and sex == 'female' and age > 16 and age < 22:
print('开始表白')
else:
print('阿姨好')
print('end....')
(3)if elif else 执行过程if条件不成立,进入elif判断,如果成立执行程序后结束程序不执行else,如果elif条件依旧不成立执行else中代码。
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3
else:
1 ''' 2 如果:成绩>=90,那么:优秀 3 如果成绩>=80且<90,那么:良好 4 如果成绩>=70且<80,那么:普通 5 其他情况:很差 6 7 ''' 8 score=input('your score: ') score='73' 9 score=int(score) score=73 10 if score >= 90: 11 print('优秀') 12 elif score >= 80: 13 print('良好') 14 elif score >= 70: 15 print('普通') 16 else: 17 print('很差')
二 if的嵌套(先判断条件1、在判断条件2)
if 条件1:(成立进入下一个if 不成立执行else2中代码)
if 条件2:(成立执行代码,不成立执行else1中代码)
else1:
pass
else2:
pass
1 cls='human' 2 sex='female' 3 age=18 4 is_success=False 5 if cls == 'human' and sex == 'female' and age > 16 and age < 22: 6 print('开始表白...') 7 if is_success: 8 print('在一起') 9 else: 10 print('我逗你玩呢....') 11 else: 12 print('阿姨好') 13 print('end....')
三 while循环
1、条件成立,一次执行1、2、3,一次循环
while 条件:
code1
code2
code3
....
1 count = 0 2 while count < 3: 3 print(count) 4 count += 1
2、while 中也可以加条件判断通过 break 和 continue控制(break跳出本层循环,continue跳出本次循环)
1、while+break:break的意思是终止掉当前层的循环,.执行其他代码 循环输出1、2 不输出3 while True: print('1') print('2') break print('3') 2、 while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环 ##ps:记住continue一定不要加到循环体最后一步执行的代码 n=1 while n <= 10: if n == 8: n += 1 n=9 continue print(n) n+=1 n=11
3、while与else (程序结束后会判断 如果while循环没有被break掉会执行else中代码)
while True:
inp_user=input('username>>: ')
inp_pwd=input('password>>: ')
if inp_user == user_db and inp_pwd == pwd_db:
print('login successfull')
break
else:
print('user or password error')
else:
print('其他代码')
4、contiue何事使用才有意义
while True:
if 条件1:
code1
code2
code3
continue 无意义
elif 条件1:
code1
continue 有意义
code2
code3
elif 条件1:
code1
code2
code3
continue 无意义
....
else:
code1
code2
code3
continue 无意义
5、while的循环嵌套(条件1成立进入 条件2的循环中)
while 条件1:
代码1
while 条件2:
代码2
example1 = 条件为True 通过break 跳出循环
1 user_db='egon' 2 pwd_db='123' 3 while True: 4 inp_user=input('username>>: ') 5 inp_pwd=input('password>>: ') 6 if inp_user == user_db and inp_pwd == pwd_db: 7 print('login successfull') 8 while True: 9 cmd=input('请输入你要执行的命令: ') 10 if cmd == 'q': 11 break 12 print('%s 功能执行...' %cmd) 13 break 14 else: 15 print('user or password error') 16 print('end....')
example2 = 条件使用tag标签,需要退出的时候通过给tag赋值跳出循环
1 while+tag 2 user_db='egon' 3 pwd_db='123' 4 tag=True 5 while tag: 6 inp_user=input('username>>: ') 7 inp_pwd=input('password>>: ') 8 if inp_user == user_db and inp_pwd == pwd_db: 9 print('login successfull') 10 while tag: 11 cmd=input('请输入你要执行的命令: ') 12 if cmd == 'q': 13 tag=False 14 else: 15 print('%s 功能执行...' %cmd) 16 else: 17 print('user or password error') 18 print('end....')
四: for 循环
1、for循环嵌套
for i in range(5):
print('========>第一层: %s<=========' %i)
for j in range(3):
print(' 第二层: %s' %j)
2、for与break,与while与break组合使用原理一样
names=['asb','wsb','egon','lsb','csb']
for n in names:
if n == 'egon':
break
print(n)
3、continue 也是跳出本次循环
names=['asb','wsb','egon','lsb','csb']
for n in names:
if n == 'egon':
continue
print(n)
4、当for执行结束后在else ,在没有break的前提下
names=['asb','wsb','egon','lsb','csb']
for n in names:
if n == 'egon':
break
print(n)
else:
print('=====>')

浙公网安备 33010602011771号