流程控制

一、if判断

# 1、语法一
if 条件:
#条件成立时执行的子代码块`
代码1
代码2
代码3
# 示例:
sex='female'
age=18
is_beautiful=True
if sex == 'female' and age > 16 and age < 20 and is_beautiful:
   print('开始表白。。。')
print('other code1...')
print('other code2...')
print('other code3...')

# 2、语法二
if 条件:
   # 条件成立时执行的子代码块
   代码1
   代码2
   代码3
else:
   # 条件不成立时执行的子代码块
   代码1
   代码2
   代码3
# 示例:
sex='female'
age=38
is_beautiful=True
if sex == 'female' and age > 16 and age < 20 and is_beautiful:
   print('开始表白。。。')
else:
   print('阿姨好。。。')
print('other code1...')
print('other code2...')
print('other code3...')

# 3、语法三:
if 条件1:
   if 条件2:
       代码1
       代码2
       代码3
# 示例:
sex='female'
age=18
is_beautiful=True
is_successful=True
height=1.70
if 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...')

# 4、语法四:
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=1
while 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=True
while tag:
   print(1)
   print(2)
   print(3)
   # tag=False
   break
else:
   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=True
while 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=0
while i < len(l):
   print(l[i])
   i+=1

for 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)
...
1
2
3
4
>>> 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 i in range(3):
   for j in range(4):
       print(i,j)

for i in [0,1,2]: # i=1
   for j in [0,1,2,3]: # j=1
       print(i,j)

0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3

流程控制(if、while、for)的更多相关文章

  1. 第10章 Shell编程(4)_流程控制

    5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...

  2. Shell命令和流程控制

    Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...

  3. PHP基础知识之流程控制的替代语法

    PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...

  4. Python黑帽编程2.4 流程控制

    Python黑帽编程2.4  流程控制 本节要介绍的是Python编程中和流程控制有关的关键字和相关内容. 2.4.1 if …..else 先上一段代码: #!/usr/bin/python # - ...

  5. 使用yield进行异步流程控制

    现状 目前我们对异步回调的解决方案有这么几种:回调,deferred/promise和事件触发.回调的方式自不必说,需要硬编码调用,而且有可能会出现复杂的嵌套关系,造成"回调黑洞" ...

  6. [Java入门笔记] Java语言基础(四):流程控制

    流程控制指的是在程序运行的过程中控制程序运行走向的方式.主要分为以下几种: 顺序结构 顺序结构,顾名思义,是指程序从上往下逐步顺序执行.中间没有任何的判断和跳转. 分支结构 Java提供两种分支结构: ...

  7. node基础13:异步流程控制

    1.流程控制 因为在node中大部分的api都是异步的,比如说读取文件,如果采用回调函数的形式,很容易造成地狱回调,代码非常不容易进行维护. 因此,为了解决这个问题,有大神写了async这个中间件.极 ...

  8. Shell入门教程:流程控制(1)命令的结束状态

    在Bash Shell中,流程控制命令有2大类:“条件”.“循环”.属于“条件”的有:if.case:属于“循环”的有:for.while.until:命令 select 既属于“条件”,也属于“循环 ...

  9. Oracle中PL/SQL的执行部分和各种流程控制

    Oracle中PL/SQL的执行部分和异常部分 一.PL/SQL的执行部分. 赋值语句. 赋值语句分两种,一种是定义一个变量,然后接收用户的IO赋值:另一种是通过SQL查询结果赋值. 用户赋值举例: ...

  10. swift_简单值 | 元祖 | 流程控制 | 字符串 | 集合

    //: Playground - noun: a place where people can play import Cocoa var str = "Hello, playground& ...

随机推荐

  1. 运维监控---企业级Zabbix详解_【all】

    基础LNMP环境搭建 Linux 下LNMP环境搭建 下载Zabbix 链接:https://pan.baidu.com/s/1n36esVyYAKstwnFopbV2sg 密码:izll 创建zab ...

  2. Excel 导出指定行为txt文件(VBA,宏)

    要从Excel 多个sheet内导出指定行为txt文件,懒得用C#了,写个VBA宏 Sub Export() Dim FileName As Variant Dim Sep As String Dim ...

  3. Linux命令--系统管理

    shutdown命令 Linux shutdown命令可以用来进行关机程序,并且在关机以前传送讯息给所有使用者正在执行的程序,shutdown 也可以用来重开机. 使用权限:系统管理者. 语法 shu ...

  4. Alpha Scrum2

    Alpha Scrum2 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:督促和监督团队进度.协调组内合作,前端页面编写,博客发布 林书浩.陈远 ...

  5. JAVAWEB之文件的上传下载

    文件上传下载 文件上传: 本篇文章使用的文件上传的例子使用的都是原生技术,servelt+jdbc+fileupload插件,这也是笔者的习惯,当接触到某些从未接触过的东西时,总是喜欢用最原始的东西将 ...

  6. 纯css3跑马灯demo

    我们写跑马灯一般都是用js控制定时器不断循环产生,但是定时器消耗比较大,特别是程序中很多用到定时器的时候,感觉有的时候比较卡.但是css3样式一般不会.这里主要的思路就是用css3代替js定时器实现一 ...

  7. jq实现简单的滑动解锁效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. BZOJ 1084 最大子矩阵 dp

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1084 题目大意: 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分 ...

  9. 更有效率的使用Visual Studio

    工欲善其事,必先利其器.虽然说Vim和Emacs是神器,但是对于使用Visual Studio的程序员来说,我们也可以通过一些快捷键和潜在的一些功能实现脱离鼠标写代码,提高工作效率,像使用Vim一样使 ...

  10. 分享一个ASP.NET的弹出层,比较好用!

    网上的一些弹出层的控件多了去了,我很久之前用了一个,效果还不错,但如果应用到ASP.NET的话,会出现“弹出层内的控件runat='server'失效”的情况,具体情况我也不太会描述,但就是那些onc ...