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循环 + 练习题的更多相关文章

  1. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  2. Python基础之条件语句和循环

    条件语句 Python中的条件语句分为 if ...else . if ...elif...else  以及if ...else 的嵌套使用: username = input('请输入您的用户名:' ...

  3. Python编程基础[条件语句if 循环语句 for,while](二)

    ython条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 判断条件: 执行语句……else: 执行语句…… x= ...

  4. python学习之条件语句(if循环)

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为tru ...

  5. 【Python】解析Python中的条件语句和循环语句

    1.if语句 if语句有好几种格式,比如: if condition: statement 使用 if ... else ...: if condition: statement(1) else: s ...

  6. python学习第六天 条件判断和循环

    总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...

  7. Python学习之条件判断和循环

    #coding= utf-8 # 条件判断和循环 # 如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做 age1 = 20 if age1 >= 18: prin ...

  8. Python学习-if条件语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 单分支条件语句 if 判断条件: 条件成立,执行该代码块.... 注意:与其他编程语言,如Java和C ...

  9. python学习:条件语句if、else

    条件语句: 1.if...else...; 2.if...elif...esle 举例: 1.if...else... “age_of_princal = 56   guess_age = int(i ...

随机推荐

  1. sick 激光

    sick10:TiM561-2050101https://www.sick.com/cn/zh/detection-and-ranging-solutions/2d-lidar-/tim5xx/tim ...

  2. allowMultiQueries=true mybatis 要多行sql执行,一定要注意

    allowMultiQueries=true 这个配置已经出现多次问题了,这次由于切换时多数据源,搞配置的同志不知道从哪里copy的配置,只换了我们的链接,我们之前配置了好多配置都丢失了,我的代码中有 ...

  3. python线程+队列(queue)

    ---恢复内容开始--- python的线程学习 用处 pocpiliang脚本的编写 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: _thr ...

  4. MySQL个人用户的安装配置详解

    1. 我的版本是 MySQL 5.7.26.0 ,因为据说 MySQL 8 的性能虽然强悍,但是兼容性还是有问题,而且发布时间不够长,没有普及,就暂时用着5.7版本. (1) 下载地址,选择使用msi ...

  5. 跨域请求错误: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

    今天在学习Angular 的HttpInterceptor 拦截器时,发现添加了新的headers键值之后总是报跨域错误.后台使用的是asp.net core. 检查发现,在添加了新的header之后 ...

  6. Ajax简单异步上传图片并回显

    前台代码 上传图片按钮 <a href="javascript:void(0)" onclick="uploadPhoto()">选择图片</ ...

  7. electron之打包成安装程序

    1.安装electron-winstaller npm install --save-dev electron-winstaller 2.创建一个build.js var electronInstal ...

  8. tp中打印sql,查看语句信息

    $a = self::where($where)->fetchSql(true)->select(); dump($a);

  9. 详解Intellij IDEA中.properties文件中文显示乱码问题的解决

    首先,你可能会见到如下提示: File encoding is disabled because .properties file (see Settings|Editor|File Encoding ...

  10. DELPHI10.3.1安卓照相

    DELPHI10.3.1安卓照相 解决方法: