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. Allowed memory size of 134217728 bytes exhausted解决办法(php内存耗尽报错)【简记】

    报错: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) i ...

  2. 5.2Python数据处理篇之Sympy系列(二)---Sympy的基本操作

    目录 目录 前言 (一)符号的初始化与输出设置-symbol() symbols() latex() 1.作用: 2.操作: (二)替换符号-subs(old,new) 1.说明: 2.源代码: 3. ...

  3. SQLServer之修改触发器

    修改触发器规则 修改CREATE TRIGGER语句以前创建的 DML.DDL 或登录触发器的定义.触发器是通过使用CREATE TRIGGER创建的.这些触发器可以由Transact-SQL语句直接 ...

  4. .NET CORE学习笔记系列(2)——依赖注入[8]: .NET Core DI框架[服务消费]

    原文:https://www.cnblogs.com/artech/p/net-core-di-08.html 包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的I ...

  5. js 页面history.back()返回上一页,ios 不重新加载ready的解决办法

    参考自 http://blog.csdn.net/hbts_901111zb/article/details/76691900 项目中,主页面有很多输入字段,当由主页跳转到子页面, 将子页面的字段 s ...

  6. Spring Security(三十七):Part IV. Web Application Security

    Most Spring Security users will be using the framework in applications which make user of HTTP and t ...

  7. python之shutil

    ''' shutil 用来处理 文件 文件夹 压缩包 的模块 ''' import shutil # 拷贝文件内容 shutil.copyfileobj(open('old.xml', 'r'), o ...

  8. maven 发布快照版本后的引用

    使用nexus发布快照版本后, 引用项目问题 必须 <scope>test</scope> 才能引用快照.releases 不受此限制

  9. 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)

    Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...

  10. js语言规范_ES5-6-7_个人总结

    ## **理解ES** 1. 全称: ECMAScript 2. js语言的规范 3. 我们用的js是它的实现 4. js的组成   * ECMAScript(js基础)   * 扩展-->浏览 ...