Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6991fe

Browse files
Create kc35.md
1 parent 196400f commit c6991fe

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎kc/kc35.md‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 3-5 python逻辑之程序循环while
2+
while和for相比,功能基本一样,都是用于循环,即在条件满足的情况下,重复执行同一个代码块
3+
4+
```python
5+
# 再看看用for循环的写法,for语句重复从列表中取数,依次打印出来
6+
for x in [1,2,3,4,5]:
7+
print(x) #通过for语句,循环5次,重复输出即可
8+
9+
10+
# 用while循环的写法,同for循环的结果是一样的
11+
x = 1 # x赋值1
12+
while(x < 6): # 判断 x<6吗? 不是就继续循环
13+
print(x) # 打印输出 x的值
14+
x = x + 1 # x+1 ,while不像for能自己累加
15+
```
16+
![25](https://user-images.githubusercontent.com/103555341/163700067-ea26b10e-ea9a-4492-8cac-b2a51756043b.jpg)
17+
# 最后总结 while 循环的基本结构
18+
```python
19+
# while 循环的基本结构 ( while 和 条件 组成 )
20+
while(条件满足): # 结尾一定有冒号':', 只要条件满足就重复执行循环体内部的语句
21+
语句1 # 前面有空格,语句缩进,代表语句1是while循环中的
22+
语句2 # 语句缩进,代表语句2是while循环中的
23+
....
24+
语句3 # 没有空格缩进,同while层级,不是while循环内的
25+
语句4 # 没有空格缩进,同while层级,不是while循环内的
26+
```
27+
## 输出从1开始,小于6的数字
28+
在这个代码中每循环一次x加上1,这样才能在5次循环中 x=6,打破循环的条件
29+
30+
**注意,如果不对x进行加的运算,这个就是一个死循环,程序就会一直在循环里打转,在项目中是非常可怕的事情**
31+
### 代码区 1
32+
```python
33+
1 # -- while循环演示 -----
34+
2 x = 1 # x赋值1
35+
3 while(x < 6): # 判断 x<6吗? 不是就继续循环
36+
4 print(x) # 打印输出 x的值
37+
5 x = x + 1 # x+1 ,while不像for能自己累加
38+
6
39+
7 print('end') # 没有缩进,不在循环体内部,打印结束
40+
8
41+
```
42+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
43+
```python
44+
1
45+
2
46+
3
47+
4
48+
5
49+
end
50+
```
51+
### 代码区 2
52+
```python
53+
1 # while循环演示:依次访问列表
54+
2 name = ['python', 'java', 'go','php','c++','delphi','Basic']
55+
3 x=0 #先让x=0, len(name)函数能获取name的元素个数
56+
4 while (x<len(name)): #开始循环判断
57+
5 print(name[x]) #用列表索引的方法输出列表
58+
6 x=x+1 # x叫+1,否则while就是死循环了
59+
7
60+
8 print('---end---') #跳出循环,程序结束
61+
9
62+
```
63+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
64+
```python
65+
python
66+
java
67+
go
68+
php
69+
c++
70+
delphi
71+
Basic
72+
---end---
73+
```
74+
### 代码区 3
75+
```python
76+
1 # while循环演示:打印等腰三角形
77+
2 i=0; x=10
78+
3 while(i<x): #while循环开始,直到 i>9 就停止
79+
4 print(" "*(x-i)+"* "*(i+1))
80+
5 i=i+1
81+
6
82+
```
83+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
84+
```python
85+
*
86+
* *
87+
* * *
88+
* * * *
89+
* * * * *
90+
* * * * * *
91+
* * * * * * *
92+
* * * * * * * *
93+
* * * * * * * * *
94+
* * * * * * * * * *
95+
```
96+
### 代码区 4
97+
```python
98+
1 # 练习题 定义一个自己的循环,千万不要是死循环 ,如果真是不小心执行了死循环, 点击上方工具按纽里的重启内核
99+
2
100+
```
101+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
102+
103+
104+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /