# 第一次作业

# 01-sum.py

1
2
3
4
5
6
n = int(input('请输入一个整数 : '))
sum = 0
for i in range(n):
sum += i
print('1~%d的求和结果为%d' % (n, sum))

image-20220311232309939

# 02-sort.py

1
2
3
4
5
6
7
l = []
for i in range(3):
x = int(input('请输入整数 : '))
l.append(x)
l.sort()
print(l)

image-20220311232513756

# 03-99list.py

1
2
3
4
for i in range(1, 10):
for j in range(1, i + 1):
print('%d * %d = %-2d' % (j, i, i * j), end=' ')
print('')
image-20220311232658759

# 04-pentagrams.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import turtle as t


def draw_fiveStars(len):
count = 1
while count <= 5:
t.forward(len)
t.right(144)
count += 1
len += 10
if len <= 100:
draw_fiveStars(len)


def main():
t.penup()
t.backward(100)
t.pendown()
t.pensize(2)
t.pencolor('red')
segment = 50
draw_fiveStars(segment)
t.exitonclick()


if __name__ == '__main__':
main()

image-20220311233156163

# 5-fiboSeq.py

1
2
3
4
5
l = [1, 2]
n = int(input('斐波那契数列长度 : '))
while len(l) < n:
l.append(l[len(l) - 1] + l[len(l) - 2])
print(l)
image-20220312122548101

# 6-finalNum.py

1
2
3
4
5
6
7
8
9
10
11
12
for i in range(2, 1001):
k = []
s = i
count = 0
sum = 0
for j in range(1, i):
if i % j == 0:
k.append(j)
count += 1
sum += j
if sum == i:
print(i)
image-20220312122635192

# 7-age.py

1
2
3
4
5
6
7
8
9
def age(n):
if n == 1:
c = 10
else:
c = age(n - 1) + 2
return c


print(age(5))
image-20220312122710520

# 8-printList.py

1
2
3
ls = ['one', 'two', 'three']
for i in ls[::-1]:
print(i)
image-20220312122800104

# 9-what.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import turtle
ninja = turtle.Turtle()
ninja.speed(10)
for i in range(180):
ninja.forward(100)
ninja.right(30)
ninja.forward(20)
ninja.left(60)
ninja.forward(50)
ninja.right(30)
ninja.penup()
ninja.setposition(0, 0)
ninja.pendown()
ninja.right(2)
turtle.done()

image-20220312123019467

# 10-drawClock.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from turtle import *
from datetime import *


def Skip(step):
penup()
forward(step)
pendown()


# 建立钟表的外框
def setupClock(radius):
reset()
pensize(7)
for i in range(60):
Skip(radius)
if i % 5 == 0:
forward(20)
Skip(-radius - 20)
else:
dot(5)
Skip(-radius)
right(6)


# 注册 turtle 形状,建立名字为 name 的形状
def makeHand(name, length):
reset()
Skip(-0.1 * length)
# 开始记录多边形的顶点
begin_poly()
forward(1.1 * length)
# 停止记录多边形的顶点,并与第一个顶点相连
end_poly()
# 返回最后记录的多边形
handForm = get_poly()
# 注册形状,命名为 name
register_shape(name, handForm)


def init():
global secHand, minHand, hurHand, printer
# 重置 turtle 指针向北
mode("logo")
secHand = Turtle()
makeHand("secHand", 125)
secHand.shape("secHand")
minHand = Turtle()
makeHand("minHand", 130)
minHand.shape("minHand")
hurHand = Turtle()
makeHand("hurHand", 90)
hurHand.shape("hurHand")
# shapesize 第一个参数没看到什么用,第二个参数表示几倍的长度,第 3 个参数表示 3倍的宽度
# speed(0)是最快
for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 3)
hand.speed(0)
# 建立并输出文字的 turtle 对象,printer 对象只是显示文字不显示路径,所以一直是penup 和 hideturtle
printer = Turtle()
printer.hideturtle()
printer.penup()


def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期七"]
return week[t.weekday()]


def Day(t):
return "%s %d %d" % (t.year, t.month, t.day)


def Tick():
t = datetime.today()
second = t.second + t.microsecond * 0.000001
minute = t.minute + t.second / 60.0
hour = t.hour + t.minute / 60.0
secHand.setheading(second * 6)
minHand.setheading(minute * 6)
hurHand.setheading(hour * 30)
tracer(False)
printer.fd(70)
printer.write(Week(t), align="center", font=("Courier", 14, "bold"))
printer.back(130)
printer.write(Day(t), align="center", font=("Courier", 14, "bold"))
printer.home()
tracer(True)
ontimer(Tick, 100) # 100ms 后继续调用 Tick


def main():
# 关闭动画
tracer(False)
init()
setupClock(200)
# 开启动画
tracer(True)
Tick()
done()


main()

image-20220312123415620

# 11-drawHeart.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from turtle import *
pensize(1)
pencolor('red')
fillcolor('pink')
speed(5)
up()
goto(-30, 100)
down()
begin_fill()
left(90)
circle(120,180)
circle(360,70)
left(38)
circle(360,70)
circle(120,180)
end_fill()
up()
goto(-100,-100)
down()
image-20220312123536163

# 12-drawFx.py

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-4, 4, 1024)
Y = .25 * (X + 4.) * (X + 1.) * (X - 2.)
plt.title('$f(x)=\\frac{1}{4}(x+4)(x+1)(x-2)$')
plt.plot(X, Y, c = 'g')
plt.show()
image-20220312124638549

# 13-randomHearts.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import turtle as t
import random as r


def randomcolor():
color = (r.random(), r.random(), r.random())
return color


def pink():
color = (1, r.random(), 1)
return color


def randomrange(min, max):
return min + (max - min) * r.random()


def moveto(x, y):
t.penup()
t.goto(x, y)
t.pendown()


def heart(r, a):
factor = 180
t.seth(a)
t.circle(-r, factor)
t.fd(2 * r)
t.right(90)
t.fd(2 * r)
t.circle(-r, factor)


# set canvas dimension
t.setup(800, 800, 200, 200)
t.speed(9)
t.pensize(1)
t.pencolor(randomcolor())
t.fillcolor(randomcolor())
t.penup()

for i in range(20):
t.goto(randomrange(-300, 300), randomrange(-300, 300))
t.begin_fill()
t.fillcolor(pink())
heart(randomrange(10, 50), randomrange(0, 90))
t.end_fill()
moveto(400, -400)

image-20220312124846375
更新于 阅读次数