Skip to content

Commit a13cbc7

Browse files
committed
a
1 parent 0b65fab commit a13cbc7

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

md/139.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,30 @@
66
@version
77
@date 2020/02/06
88
```
9-
9+
#### 139 定制递减迭代器
10+
11+
```python
12+
#编写一个迭代器,通过循环语句,实现对某个正整数的依次递减1,直到0.
13+
class Descend(Iterator):
14+
def __init__(self,N):
15+
self.N=N
16+
self.a=0
17+
def __iter__(self):
18+
return self
19+
def __next__(self):
20+
while self.a<self.N:
21+
self.N-=1
22+
return self.N
23+
raise StopIteration
24+
25+
descend_iter=Descend(10)
26+
print(list(descend_iter))
27+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
28+
```
29+
30+
核心要点:
31+
32+
1 `__nex__ `名字不能变,实现定制的迭代逻辑
33+
34+
2 `raise StopIteration`:通过 raise 中断程序,必须这样写
35+

md/140.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,45 @@
66
@version
77
@date 2020/02/07
88
```
9-
9+
10+
#### 140 turtle绘制奥运五环图
11+
12+
turtle绘图的函数非常好用,基本看到函数名字,就能知道它的含义,下面使用turtle,仅用15行代码来绘制奥运五环图。
13+
14+
1 导入库
15+
16+
```python
17+
import turtle as p
18+
```
19+
20+
2 定义画圆函数
21+
22+
```python
23+
def drawCircle(x,y,c='red'):
24+
p.pu()# 抬起画笔
25+
p.goto(x,y) # 绘制圆的起始位置
26+
p.pd()# 放下画笔
27+
p.color(c)# 绘制c色圆环
28+
p.circle(30,360) #绘制圆:半径,角度
29+
```
30+
31+
3 画笔基本设置
32+
33+
```python
34+
p = turtle
35+
p.pensize(3) # 画笔尺寸设置3
36+
```
37+
38+
4 绘制五环图
39+
40+
调用画圆函数
41+
42+
```python
43+
drawCircle(0,0,'blue')
44+
drawCircle(60,0,'black')
45+
drawCircle(120,0,'red')
46+
drawCircle(90,-30,'green')
47+
drawCircle(30,-30,'yellow')
48+
49+
p.done()
50+
```

0 commit comments

Comments
 (0)