python学习-7 条件语句 while循环 + 练习题
1.死循环
while 1 == 1:
print('ok')
结果是一直循环
2.循环
count = 0
while count < 10:
print(count)
count = count +1
print(error)
3.练习题
~ 使用while循环输出1 2 3 4 5 6 8 9 10
count = 1 while count <= 10 : # 或者count < 11
if count == 7:
print( ) # 也可以添加pass,什么也不执行
else:
print(count)
count = count + 1
执行结果:
1
2
3
4
5
6 8
9
10 Process finished with exit code 0
~ 求1-100的所有数的和
a = 1
b = 0
while a < 101:
b = b + a
a = a + 1
print(b)
输出结果:
5050 Process finished with exit code 0
~求1-100内所有的奇数
n = 1
while n < 101:
js = n % 2
if js == 0:
print( )
else:
print(n)
n = n + 1
输出结果:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
~ 求1-100内所有的偶数
a = 1
while a < 101:
b = a % 2
if b == 0:
print(a)
else:
pass
a = a + 1
输出结果:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100 Process finished with exit code 0
python学习-7 条件语句 while循环 + 练习题的更多相关文章
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- Python基础之条件语句和循环
条件语句 Python中的条件语句分为 if ...else . if ...elif...else 以及if ...else 的嵌套使用: username = input('请输入您的用户名:' ...
- Python编程基础[条件语句if 循环语句 for,while](二)
ython条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 判断条件: 执行语句……else: 执行语句…… x= ...
- python学习之条件语句(if循环)
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为tru ...
- 【Python】解析Python中的条件语句和循环语句
1.if语句 if语句有好几种格式,比如: if condition: statement 使用 if ... else ...: if condition: statement(1) else: s ...
- python学习第六天 条件判断和循环
总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...
- Python学习之条件判断和循环
#coding= utf-8 # 条件判断和循环 # 如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做 age1 = 20 if age1 >= 18: prin ...
- Python学习-if条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 单分支条件语句 if 判断条件: 条件成立,执行该代码块.... 注意:与其他编程语言,如Java和C ...
- python学习:条件语句if、else
条件语句: 1.if...else...; 2.if...elif...esle 举例: 1.if...else... “age_of_princal = 56 guess_age = int(i ...
随机推荐
- 自动化测试报告浅谈之ExtentReports
我们在进行自动化测试时,往往需要有相应的测试报告,比如junit,testng,reportng等等,有会有自带的测试报告,那为什么我要在这边提ExtentReports?首先,我们来看看其它几种测试 ...
- c++ demo code
/* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...
- 微信小程序之简单记账本开发记录(二)
1.打开开发者工具 2.从微信公众平台上获取到appid,或者使用测试号. 项目的大致目录如下: 一个小程序主体部分由三个文件组成,必须放在项目的根目录中 以app为开头的文件名用来布置作用于整个项目 ...
- [BJOI2019]光线——递推
题目链接: [BJOI2019]光线 设$F_{i}$表示从第$1$面玻璃上面向下射入一单位光线,穿过前$i$面玻璃的透光率. 设$G_{i}$表示从第$i$面玻璃下面向上射入一单位光线,穿过前$i$ ...
- LinkedBlockingQueue和ArrayBlockingQueue的异同
相同: 1.LinkedBlockingQueue和ArrayBlockingQueue都实现了BlockingQueue接口: 2.LinkedBlockingQueue和ArrayBlocking ...
- Dubbo系列(三)dubbo的核心技术--RPC调用
dubbo的核心技术--RPC调用:分为俩部分RPC协议Protocol和方法调用Invoke: 一.RPC协议Protocol(Remote Procedure Call)远程过程调用协议 1.我们 ...
- Nice的应用整理
笔记清单 印象笔记 -- 知识,账号 Typora -- markdown编写 xmind 思维导图 sm.ms 图床 processon 流程图 愿望清单 滴答清单 -- 执行力,备忘录 账号 账号 ...
- error:Cannot pull with rebase
原文文链接:https://blog.csdn.net/u012385190/article/details/70670213git 执行git pull –rebase报错误如下: error: C ...
- linux内核中的wait_event_interruptible_timeout接口解析
1. 原型 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \ ({ \ long __ret = time ...
- qt application logging
“AnalysisPtsDataTool201905.exe”(Win32): 已加载“F:\OpencvProject\ZY-Project\x64\Debug\AnalysisPtsDataToo ...