if判断

  if判断想执行第一个条件,if后的判断必须是True

1 什么是if判断
  判断一个条件如果成立则做...不成立则做....
2 为何要有if判断
  让计算机能够像人一样具有判断的能力
3 如何用if判断

语法1:

  if 条件1:
    code1
    code2
    code3
    ......

语法2:if-else

  if 条件:
    code1
    code2
    code3
    ......
  else:
    code1
    code2
    code3
    ......

语法3:嵌套

   if 条件1:
    if 条件2:
      code1
      code2
      code3
    code2
    code3
    ......

语法4:elif   

     if 条件1:
    子代码块1
   elif 条件2:
    子代码块2
   elif 条件3:
    子代码块3
   elif 条件4:
    子代码块4
    .........
   else:
    子代码块5

 代码练习

score = input('your score>>: ')
score=int(score)
if score >= 90:  #不满足的必然小于90,elif只需要>=80就行,不用判断与90的关系
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')

while循环判断

1. 什么是循环
  循环指的是一个重复做某件事的过程

2. 为何要有循环
  为了让计算机能够像人一样重复做某件事
3. 如何用循环

while循环的语法:while循环又称为条件循环,循环的次数取决于条件

     while 条件:
      子代码1
      子代码2
      子代码3

登录账号密码验证:

  print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
else:
print('user or password err')
print('end...')

结束while循环的方式:

方式一:操作while循环的条件让其结束

  控制条件的两种方式:

判断count:
count=1
while count < 6:
print(count)
count+=1 判断布尔
tag=True
count=1
while tag:
if count > 5:
tag=False
print(count)
count+=1
  print('start....')
tag=True
while tag:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
tag=False
else:
print('user or password err') print('end...')

 方式二: break强行终止本层循环

  print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err') print('end...')

输错三次账号或密码直接退出

  方式一:
print('start....')
count=0
while count <= 2: #count=3
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1 print('end...') 方式二:
print('start....')
count=0
while True:
if count == 3:
print('输错的次数过多傻叉')
break
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1 print('end...')
continue :结束本次循环,直接进入下一次
break :结束本层循环
count=1
while count < 6:
if count == 4:
count+=1 #如果不加一,会进入死循环
continue # 只能在cotinue同一级别之前加代码,之后的代码不会执行
print(count) #print 不能放在if之前,否则排除不了4
count+=1 while True:
print('')
print('')
print('')
continue # 不应该将continue作为循环体最后一步执行的代码反正也会重新开始循环,相当于多此一举
while+else
count=1
while count < 6:
if count == 4:
break
print(count)
count+=1
else:
print('会在while循环没有被break终止的情况下执行')#如果while循环被终止了的话,else后的代码不会执行

输错三次则退出之while+else的应用代码:

  print('start....')
count=0
while count <= 2: #count=0,1,2 3的时候是第四次
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('user or password err')
count+=1
else:
print('输错的次数过多') print('end...')
while循环的嵌套,内部循环输入4,直接退出程序,即退出两个循环

复杂版:
  name_of_db='egon'
pwd_of_db=''
print('start....')
count=0
while count <= 2: #count=3
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == name_of_db and pwd == pwd_of_db:
print('login successful')
while True:
print("""
1 浏览商品
2 添加购物车
3 支付
4 退出
""")
choice=input('请输入你的操作: ') #choice='1'
if choice == '':
print('开始浏览商品....')
elif choice == '':
print('正在添加购物车....')
elif choice == '':
print('正在支付....')
elif choice == '':
break
break
else:
print('user or password err')
count+=1
else:
print('输错的次数过多') print('end...')
 简洁版:定义tag变量控制所有循环
 name_of_db='egon'
pwd_of_db=''
tag=True
print('start....')
count=0
while tag:
if count == 3:
print('尝试次数过多')
break
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == name_of_db and pwd == pwd_of_db:
print('login successful')
while tag:
print("""
1 浏览商品
2 添加购物车
3 支付
4 退出
""")
choice=input('请输入你的操作: ') #choice='1'
if choice == '':
print('开始浏览商品....')
elif choice == '':
print('正在添加购物车....')
elif choice == '':
print('正在支付....')
elif choice == '':
tag=False else:
print('user or password err')
count+=1 print('end...')

for循环主要用于循环取值

普通方法:

 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
i=0
while i < len(student):  #len为列表的方法,相当于Java中数组的长度方法
print(student[i])
i+=1 

用for循环取值:

  student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']

 #打印每一个列表元素
for item in student:
print(item) #打印字符串的每个字符
for item in 'hello':
print(item) #只能取出key,然后通过key来去对应的值
dic={'x':444,'y':333,'z':555}
for k in dic:
print(k,dic[k]) #range:范围,1-10,取头不取尾,步数为3;
for i in range(1,10,3):
print(i)
>>1,4,7 #省略起点0,默认步长为1,结果是0-9,十个数
for i in range(10):
print(i)

取出编号并对应老师名:

 student=['egon','虎老师','lxxdsb','alexdsb','wupeiqisb']
for i in range(len(student)):#len(student)=5,即range(0,5),i=0,1,2,3,4
print(i,student[i]) 运行结果:

  0 egon
  1 虎老师
  2 lxxdsb
  3 alexdsb
  4 wupeiqisb

流程控制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. vue给不同环境配置不同打包命令

    第1步:安装cross-env 1 npm i --save-dev cross-env 第2步:修改各环境下的参数 在config/目录下添加test.env.js.pre.env.js. 修改pr ...

  2. Python编码规范(PEP8)及奇技淫巧(不断更新)

    https://blog.csdn.net/MrLevo520/article/details/69155636

  3. SQL AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  4. 外部访问docker中的MySQL

    注:192.168.1.203机器上装有docker,容器在该机器上 mysql> use mysql; mysql> update user set authentication_str ...

  5. Java集合之Map和Set源码分析

    以前就知道Set和Map是java中的两种集合,Set代表集合元素无序.不可重复的集合:Map是代表一种由多个key-value对组成的集合.然后两个集合分别有增删改查的方法.然后就迷迷糊糊地用着.突 ...

  6. Linux内存管理 (26)内存相关工具

    1. vmstat 参照<Linux CPU占用率监控工具小结-vmstat> 2. memstat memstat可以通过sudo apt install memstat安装,安装包括两 ...

  7. iOS开发基础-UITableView基本属性

    设置 UITableView 中 cell 的背景颜色. 示例1:通过 backgroundView 设置. UIView *view1 = [[UIView alloc] init]; view1. ...

  8. 在Bootstrap开发框架的工作流模块中实现流程完成后更新资料状态处理

    在开发查看流程表单明细的时候,在Web界面中,我们往往通过使用@RenderPage实现页面内容模块化的隔离,减少复杂度,因此把一些常用的如审批.撤销.会签.阅办等等的流程步骤都放到了通用处理的页面V ...

  9. OSGI打安装包步骤(底包制作)

     相关资源下载 equinox-SDK-LunaSR2  : https://pan.baidu.com/s/1xOzZZ3_VAuQJ3Zfp4W8Yyw 提取码: gjip gemini-web- ...

  10. gorose使用示例

    package main import ( "fmt" "github.com/gohouse/gorose" //import Gorose _ " ...