博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
04 if条件判断 流程控制
阅读量:7211 次
发布时间:2019-06-29

本文共 5521 字,大约阅读时间需要 18 分钟。

条件判断 if 

语法一: if 条件:     # 条件成立时执行的子代码块         代码1         代码2         代码3
示例:sex='female'age=18is_beautiful=Trueif sex == 'female' and age > 16 and age < 20 and is_beautiful:    print('开始表白。。。')print('other code1...')print('other code2...')print('other code3...')
语法二: if 条件:     # 条件成立时执行的子代码块     代码1         代码2         代码3 else:     # 条件不成立时执行的子代码块      代码1       代码2       代码3
示例:sex='female'age=38is_beautiful=Trueif sex == 'female' and age > 16 and age < 20 and is_beautiful:    print('开始表白。。。')else:    print('阿姨好。。。')print('other code1...')print('other code2...')print('other code3...')
语法三: if 条件1:       if 条件2:             代码1     代码2             代码3
示例:sex='female'age=18is_beautiful=Trueis_successful=Trueheight=1.70if sex == 'female' and age > 16 and age < 20 and is_beautiful \        and height > 1.60 and height < 1.80:     print('开始表白。。。')    if is_successful:        print('在一起。。。')    else:        print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')else:    print('阿姨好。。。')print('other code1...')print('other code2...')print('other code3...')
语法四: if 条件1:       代码1       代码2       代码3 elif 条件2:       代码1       代码2       代码3 elif 条件3:       代码1       代码2       代码3 ....... else:       代码1       代码2       代码3
示例:如果成绩 >= 90,那么:优秀如果成绩 >= 80且 < 90, 那么:良好 如果成绩 >= 70且 < 80, 那么:普通其他情况:很差score = input('please input your score: ')  # score='100'score = int(score)if score >= 90:    print('优秀')elif score >= 80:    print('良好')elif score >= 70:    print('普通')else:    print('很差')

流程控制:while循环

语法: while 条件:       代码1       代码2       代码3
while True:    name=input('please input your name: ')    pwd=input('please input your password: ')    if name == 'egon' and pwd == '123':        print('login successful')    else:        print('username or password error')
结束while循环的两种方式 方式一:条件改为False,       在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效
tag=True    while tag:        name=input('please input your name: ')        pwd=input('please input your password: ')            if name == 'egon' and pwd == '123':            print('login successful')            tag=False        else:            print('username or password error')            print('===>')
方式二:while+break       break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环
while True:        name=input('please input your name: ')        pwd=input('please input your password: ')            if name == 'egon' and pwd == '123':            print('login successful')            break        else:            print('username or password error')            print('===>>>>>')        print('===>>>>>')
while+continue:结束本次循环,直接进入下一次循环
示例一count=1while count < 6: #count=6    if count == 4:        count += 1        continue            print(count)    count+=1
示例二:while True:    name=input('please input your name: ')    pwd=input('please input your password: ')    if name == 'egon' and pwd == '123':        print('login successful')        break    else:        print('username or password error')        # continue # 此处加continue无用
了解知识 while + else: while 条件:       代码1       代码2       代码3 else:       在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码
tag=Truewhile tag:    print(1)    print(2)    print(3)    # tag=False    breakelse:    print('else的代码')

while 条件1:

while 条件2:    

  代码1    
  代码2    
  代码3

示范一:while True:    name=input('please input your name: ')    pwd=input('please input your password: ')    if name == 'egon' and pwd == '123':        print('login successful')        while True:            print("""            0 退出            1 取款            2 转账            3 查询            """)            choice=input('请输入您要执行的操作:') #choice='1'            if choice == '0':                break            elif choice == '1':                print('取款。。。')            elif choice == '2':                print('转账。。。')            elif choice == '3':                print('查询')            else:                print('输入指令错误,请重新输入')        break    else:        print('username or password error')
示范二:tag=Truewhile tag:    name=input('please input your name: ')    pwd=input('please input your password: ')    if name == 'egon' and pwd == '123':        print('login successful')        while tag:            print("""            0 退出            1 取款            2 转账            3 查询            """)            choice=input('请输入您要执行的操作:') #choice='1'            if choice == '0':                tag=False            elif choice == '1':                print('取款。。。')            elif choice == '2':                print('转账。。。')            elif choice == '3':                print('查询')            else:                print('输入指令错误,请重新输入')    else:        print('username or password error')

流程控制:for循环

  for循环的强大之处在于循环取值
l=['a','b','c','d','e']i=0while i < len(l):    print(l[i])    i+=1for x in l: # x='b'    print(x)dic={
'name':'egon','age':18,'gender':'male'}for x in dic: print(x,dic[x])
for + break
nums=[11,22,33,44,55]for x in nums:    if x == 44:        break    print(x)
for + continue
nums=[11,22,33,44,55]for x in nums:    if x == 22 or x == 44:        continue    print(x)
for + else
names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']for name in names:    if name == 'kevin_dsb':        break    print(name)else:    print('======>')
for+ range()
range的用法>>> range(1,5)[1, 2, 3, 4]>>> for i in range(1,5):...     print(i)...1234>>> range(1,5,1)[1, 2, 3, 4]>>> range(1,5,2) # 1 3[1, 3]
for i in range(5): # 0 1 2 3 4     print(i)
for嵌套
for嵌套for i in range(3):    for j in range(4):        print(i,j)执行结果:0 00 10 20 31 01 11 21 32 02 12 22 3
 

转载于:https://www.cnblogs.com/zhouyongv5/p/10578165.html

你可能感兴趣的文章
[deviceone开发]-多种样式下拉菜单demo
查看>>
大规模集群中Docker镜像如何分发管理?试试Uber刚开源的Kraken
查看>>
百度举办第七届技术开放日,揭秘春晚红包技术支撑
查看>>
从图形到像素:前端图形编程技术概览
查看>>
必须知道的app测试与web测试的区别
查看>>
可信云认证累计达20项,腾讯云技术创新能力再获专业认可
查看>>
十周后,62%的PHP网站将运行在一个不受支持的PHP版本上
查看>>
战胜阿里和腾讯,Ripple已经获得200家跨境支付客户!
查看>>
推销自己的前端技术书籍
查看>>
Visual Studio 15.8 Preview 3支持多点编辑功能
查看>>
Pravega应用实战:为什么云原生特性对流处理很重要?
查看>>
Amazon发布可持续性数据集,可用于多个领域的数据分析
查看>>
SendGrid是如何扩展它的邮件传送系统的
查看>>
Oracle发布Oracle数据库的官方Node.js驱动node-oracledb
查看>>
Spring 5.0 GA版本发布,支持JDK9及反应式编程
查看>>
wemall app商城源码Android之支付宝通知处理类
查看>>
利用已有的大数据技术,如何构建机器学习平台
查看>>
Stream从Python切换到Go的原因
查看>>
Python将迁移到GitHub
查看>>
Linux系统安装MySql步骤及截屏
查看>>