day05-控制流程之if/while/for
控制流程之if判断
if 其实就是根据条件来做出不同的反应,如果这样就这样干,如果那样就那样干
1. 如果:成绩 > 90,那么:优秀(if...)
score = 91
if score > 90:
print('优秀')
2. 如果:成绩>90,那么:优秀,否则:良好(if...else...)
score = 89
if score > 90:
print("优秀")
else:
print("良好")
3. 如果:成绩>90,那么:优秀;如果:成绩<60,那么:不及格,否则:良好(if...elif...else)
score = int(input("enter score>>>"))
if score > 90:
print("优秀")
elif score < 60:
print("不及格")
else:
print("良好")
4. 如果:成绩>90,那么:优秀;如果:成绩<60,那么:不及格,否则:良好(if嵌套)
score = int(input("enter score>>>"))
if score > 60:
if score < 90:
print("良好")
else:
print("优秀")
else:
print("不及格")
控制流程之while循环
循环就是一个重复的过程,while循环又称为条件循环,当条件满足时进入循环体,不满足时退出。
while 条件:
# 循环体
# 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
# 如果条件为假,那么循环体不执行,循环终止
1. 打印100以内的整数(while...)
count = 1
while count < 101:
print(count,end=',')
count += 1
2. 打印100以内(除50之外)的整数(while...continue)
count = 1
while count < 101:
if count == 50:
count += 1
continue # continue跳出本次循环,直接进入下一次循环
print(count, end=',')
count += 1
3. 猜年龄,直到猜中后跳出循环(while...break)
real_name = 23
while 1:
enter_name = int(input("请猜下我的年龄:"))
if enter_name == real_name:
print("恭喜你,猜对了")
break # break跳出整个循环
elif enter_name > real_name:
print("猜大了,再猜小点")
else:
print("猜小了,再猜大点")
4. 当while循环没有被break掉的时候,会执行else下面的代码(while...else)
n = 1
while n < 3:
print(n)
n += 1
else:
print('else会在while没有被break时才会执行else中的代码')
控制流程之for循环
for循环的循环次数受限于容器类型的长度,而while循环的循环次数需要自己控制
for i in 列表or字典:
i就是列表中的每一个元素
**1. 打印列表中的值(for...) **
game_list =['chaoji','CF','xiaoxiaole','lianliankan','tank']
for i in game_list:
print(i)
2. 打印列表元素'CF'前的所有元素(for...break)
game_list =['chaoji','CF','xiaoxiaole','lianliankan','tank']
for i in game_list:
if i == 'CF':
break # break跳出整个循环
print(i)
3. 打印列表中除'CF'之外的元素(for...continue)
game_list =['chaoji','CF','xiaoxiaole','lianliankan','tank']
for i in game_list:
if i == 'CF':
continue # continue跳出本次循环,直接进入下一次循环
print(i)
4. for循环没有被break的时候,执行else里的代码(for...else)
game_list =['chaoji','CF','xiaoxiaole','lianliankan','tank']
for i in game_list:
print(i)
else:
print("for循环没有被break的时候,执行else里的代码")
5. for循环嵌套
'''打印九九乘法表'''
for i in range(1, 10):
for j in range(1, i+1):
print(f"{i}*{j}={i*j}", end='\t')
print('\n', end='')
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
day05-控制流程之if/while/for的更多相关文章
- day04控制流程之if判断
一.控制流程之if判断 1.什么是if判断 判断一个条件如果成立则...不成立则... 2.为何要有if判断 让计算机能够像人一样具有判断能力 3.如何用if判断 ''' # 语法1: ''' if ...
- 控制流程之while循环, for循环
条件循环:while,语法如下 while 条件: # 循环体 # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件... # 如果条件为假,那么循环体不执行,循环终止死循环 基本使用 ...
- 赋值,逻辑,运算符, 控制流程之if 判断
赋值运算 (1). 增量运算 age += 1 # age = age + 1 print(age) age -= 10 # age = age - 10 (2).交叉赋值 x = 111 y = 2 ...
- 控制流程之if判断与while、for循环
一.if判断 1.什么是if判断? 接收用户输入的名字 接受用户输入的密码 如果用户输入的名字=正确的名字 并且 用户输入的密码=正确的密码 告诉用户登录成功 否则, 告诉用户登录失败 2.为何要有i ...
- Day01:Python入门
一.编程与编程语言 编程的目的是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而让计算机能像人脑一样自动执行工作. 能被计算机所识别的表达方式是编程语言,python就是一门编程语 ...
- Java 自定义异常(转载)
1.异常的分类 1. 非运行时异常(Checked Exception) Java中凡是继承自Exception但不是继承自RuntimeException的类都是非运行时异常. 2. 运行时异常(R ...
- for循环,数字、字符串和列表内置方法
目录 控制流程之for循环 基本语法 for+break for+continue for+else for循环打印lodaing 数字类型内置方法 整型 int 浮点型 float 字符串内置方法 ...
- 控制流程-if/while/for
目录 一.控制流程之if判断 1.单分支结构 2.双分支结构 3.多分支结构 二.控制流程之while循环 1.基本使用 2.break 3.continue 三.流程控制之for循环 1.break ...
- shell编程之awk命令详解
shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...
随机推荐
- open redis port for remote connections
edit /etc/redis.conf Add below line after bind 127.0.0.1, then try redis-cli -h xxx.xxx.xxx.xxx ping ...
- access-list/eigrp等 反掩码计算
access-list/eigrp等 反掩码计算 原则:地址部分,相同的照写,不同的写"0" 反掩码部分,相同的写"0",不同的写"1&quo ...
- [Angular] New in V6.1
Router Scroll Position Restoration: remember and restore scroll position as the user navigates aroun ...
- Windows 由于无法验证发布者,windows阻止控件安装怎么办
1 打开Internet选项 2 下载未签名的ACTIVEX控件-设为启动
- ORA-00600: internal error code, arguments: [kkqtSetOp.1]
新数据库从32升级到64位的11G 11 2 0 3 有条SQL 语句运行的时候会导致内部错误. 使用PL/SQL DEVELOPER 查询该语句的运行机会 按F5键 就激发了这个ORA600 单击此 ...
- Servlet-SrpingMVC 生成验证码
在SpringMVC中配置生成验证码: import org.springframework.stereotype.Controller; import org.springframework.web ...
- STM32跑马灯
#include "stm32f10x.h" #include "led.h" #include "delay.h" #include &q ...
- ERROR (ConnectionError): HTTPConnectionPool (Caused by <class 'socket.error'>: [Errno 111] Connecti
感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- sed 之 模式空间 & 保持空间
模式空间:容纳当前输入行的缓冲区: 保持空间:作为辅助的一个缓冲区,可以和模式空间进行交互,但是命令不能直接作用于保持空间. 由上面定义可以知道,模式空间和保持空间是两个独立的缓冲区,可以进行交互,命 ...
- Silverlight访问数据库大全(转)
Silverlight访问数据库大全 Silverlight访问数据库大全 Posted on 2010-06-13 17:25 moss_tan_jun 阅读(1917) 评论(0) 编辑 收藏 最 ...