当前位置:网站首页>学习总结day5
学习总结day5
2022-08-05 05:13:00 【非鱼丶丶】
python循环语句关键字
一:continue和break
continue和break都是循环体关键字(只出现在循环中)
1)continue - 结束一次循环(结束当次循环)
在执行循环体的时候如果遇到continue,那么这次循环结束,直接进入下次循环
2)break - 结束整个循环
for x in range(5):
if x % 2 == 0:
continue
print(x)
for m in range(5):
if m > 3:
break
print(m)
2.while使用套路
例:如果num是偶数就打印偶数,是奇数就打印奇数,如果能被4整除,打印4的倍数
while True:
每次循环需要做的事情(重复执行代码)
if 循环结束条件:
break
# 猜数字游戏,随机产生一个数(0-100),让用户猜测
from random import randint
num = randint(1, 100)
count = 0
while count < 7:
a = int(input('输入数字'))
count += 1
if a == num:
print('正确')
break
else:
if a > num:
print('大了')
else:
print('小了')
print('cai')
二:循环else
完整的for循环:
for 变量 in 序列:
循环体
else:
代码段
完整的while循环:
while 条件语句:
循环体
else:
代码段
1)else不会影响循环
2)如果else对应的循环是自然结束,else后面的代码段会执行;
如果其循环是因为遇到break而结束的,那么else后面的代码在循环结束后不会执行
边栏推荐
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- 【记一下1】2022年6月29日 哥和弟 双重痛苦
- 【过一下12】整整一星期没记录
- 数据库 单表查询
- Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
- Matplotlib(二)—— 子图
- [Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
- 学习总结week3_4类与对象
- 类的底层机制
- Requests库部署与常用函数讲解
猜你喜欢
shell函数
server disk array
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
[cesium] 3D Tileset model is loaded and associated with the model tree
[cesium] element highlighting
Lecture 5 Using pytorch to implement linear regression
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
MySQL Foundation (1) - Basic Cognition and Operation
vscode+pytorch use experience record (personal record + irregular update)
ESP32 485 Illuminance
随机推荐
入口点注入
【过一下14】自习室的一天
1.3 mysql批量插入数据
Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
flex布局青蛙游戏通关攻略
【记一下1】2022年6月29日 哥和弟 双重痛苦
Matplotlib(三)—— 实践
Transformation 和 Action 常用算子
A blog clears the Redis technology stack
Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
Difference between for..in and for..of
shell函数
day11-函数作业
Matplotlib(一)—— 基础
Flutter learning - the beginning
【过一下11】随机森林和特征工程
Wise Force Deleter强制删除工具
02.01-----参数的引用的作用“ & ”
Flutter learning 2-dart learning
ES6 生成器