昨天也就是2023-03-27我终于学完了《python语言程序设计》2018版的前6章。
从2020年到今天。三年。别人13天。我用了3年。
感觉这3年。我学到了螺丝可以拧螺母,拧完的螺丝可以安装在工件上。然后把工件在组装成单元。然后再链接这些单元。
第7章开始。我将进入如何为自己的程序设计单元,设计工件,设计螺丝。
程序学习我开始翻过第一个台阶了。
下面是第6章编程题的33题到48题的代码。都是我自己写的。仅供各位同学参考,
谢谢
另外解题步骤和我自己的日记暂不对外分享。
from math import *
def area(s):
area_num = 5 * pow(s, 2) / (4 * tan(pi / 5))
print(f"The area of the pentagon is {round(area_num, 4)}")
def main():
s = eval(input("Enter the side: "))
area(s)
main()
from math import *
side_contents = {'triangle': 3, 'pentagon': 5, 'rectangle': 4, 'hexagon': 6}
def calculate_resul(kinds_polygon):
print("All kinds polygon for area: ")
for k, v in kinds_polygon.items():
print(f'The {k} area of the {str(v)} is {area_calculate(v)}.')
def area_calculate(v):
side_length = eval(input('Enter the side: '))
area = (v * pow(side_length, 2)) / (4 * tan(pi / v))
return area
calculate_resul(side_contents)
from test_06_code_note import *
"""
test_06_code_note code:
def getRandomCharacter(ch1, ch2):
return chr(randint(ord(ch1), ord(ch2)))
def getRandomLowerCaseLetter():
return getRandomCharacter('a', 'z')
def getRandomUpperCaseLetter():
return getRandomCharacter('A', 'Z')
def getRandomDigitCharacter():
return getRandomCharacter('0', '9')
def getRandomASCIICharacter():
return chr(randint(0, 127))
"""
count = 0
for i in range(1, 10001):
# print(getRandomUpperCaseLetter())
if getRandomUpperCaseLetter() == "A":
count += 1
print("A text is show number is", count)
from test_06_code_note import *
count = 0
for i in range(1, 100):
print(getRandomUpperCaseLetter(), end=" ")
count += 1
if count % 10 == 0:
print()
from test_06_code_note import *
from turtle import *
penup()
def coordinate(x):
return forward(20)
for j in range(1, 15):
penup()
goto(0, -20 * j)
for i in range(1, 15):
penup()
write(getRandomLowerCaseLetter(), font=('', 18, ''))
coordinate(i)
hideturtle()
done()
from turtle import *
def drawLine(x1, y1, x2, y2, color_num="black", size=1):
penup()
color(color_num)
goto(x1, y1)
pensize(size)
pendown()
goto(x2, y2)
hideturtle()
drawLine(10, 10, 30, 50, "red", 30)
done()
from turtle import *
def draw_rec(dis_num, number, color_num="black", size=1):
color(color_num)
pensize(size)
for i in range(1, number + 1):
right(360 / number * 2)
forward(dis_num)
hideturtle()
draw_rec(200, 5, "red", 10)
done()
from turtle import *
def drawRectangle(color_num="black", x=0, y=0, width_num=30, height_num=30):
color(color_num)
penup()
goto(x, y)
pendown()
for i in range(1, 3):
forward(width_num)
right(90)
forward(height_num)
right(90)
hideturtle()
def drawCircle(color_num="black", x=0, y=0, radius=50):
penup()
goto(x, y)
pendown()
circle(radius)
drawRectangle("red", 0, 0, 30, 70)
drawCircle("red", 50, 50, 100)
done()
.
from turtle import *
from random import *
def draw_rectangle(x, y, height_num, width_num):
penup()
# test rectangle coordinate
# goto(x, y)
# dot(6, "red")
star_x1 = width_num / 2
star_y1 = height_num / 2
x_distance = x - star_x1
goto(x_distance, abs(y) + abs(star_y1))
pendown()
for i in range(2):
forward(width_num)
right(90)
forward(height_num)
right(90)
hideturtle()
def draw_circle(x, y, radius):
penup()
# goto(x,y)
# dot(8, "blue")
goto(x, y - radius)
pendown()
circle(radius)
hideturtle()
def draw_random_point(x, y, number):
for i in range(1, number):
if x < 0:
x_num = randint(x,1)
else:
x_num = randint(1, x)
y_num = randint(1, y)
penup()
goto(x_num, y_num)
dot(3, "red")
draw_rectangle(-75, 0, 100, 100)
draw_circle(50, 0, 50)
draw_random_point(70,30,30)
draw_random_point(-120,30,30)
done()
from turtle import *
from math import *
speed(30)
def draw_sin_tur(num1, num2):
penup()
for x in range(num1, num2):
color("red")
goto(x, 50 * sin((x / 100) * 2 * pi))
pendown()
hideturtle()
done()
draw_sin_tur(-176, 176)
from turtle import *
from math import *
"""
2023.03.26_19:52
06.43.01version
2023.03.26_20:00
"""
def draw_sin_tur(num1, num2):
speed(30)
penup()
for x in range(num1, num2):
color("red")
goto(x, 50 * sin((x / 100) * 2 * pi))
pendown()
def draw_cos_tur(num1, num2):
speed(30)
penup()
for y in range(num1, num2):
color("blue")
goto(y, 50 * cos((y / 100) * 2 * pi))
pendown()
hideturtle()
done()
draw_sin_tur(-175, 176)
draw_cos_tur(-175, 176)
from turtle import *
def draw_cur_tur(num1, num2):
speed(20)
penup()
goto(0, -20)
for i in range(num1, num2):
goto(i, i ** 2)
pendown()
draw_cur_tur(-18, 19)
hideturtle()
done()
from turtle import *
# 该代码的效果我觉得有待推敲。将来有机会再做一次的时候再说吧
def drawPolygon(x=0, y=0, radius=50, numberOfSides=3):
penup()
goto(x, y)
dot(6, "red")
goto(x, y - radius)
pendown()
circle(radius, steps=numberOfSides)
drawPolygon(120, 10, 50, 3)
drawPolygon(0, 10, 50, 8)
drawPolygon(180, 10, 50, 6)
drawPolygon(240, 10, 50, 7)
hideturtle()
done()
画六边形并且连接每个点。
from turtle import *
# 我是个数学差生,我不知道用什么办法直接点对点的连接六边形的各个点。我用的是一个函数画出了三角形六边形进行连接
# 另外这里有一个程序上的问题。不知道大家是否能够发现。如果发现了。给我留言哦。增加点我的人气。嘿嘿
def draw_poly_num(x, y, radius, side_num, angle):
penup()
goto(x, y)
dot(6, "red")
# write(f"{x},{y}")
goto(x - radius, y)
right(angle)
pendown()
circle(radius, steps=side_num)
draw_poly_num(0, 10, 50, 6, 90)
draw_poly_num(0, 10, 50, 3, 0)
draw_poly_num(100, 10, 50, 3, 180)
draw_poly_num(26, 10, 28, 3, 30)
draw_poly_num(28, 10, 28, 3, 120)
draw_poly_num(26, 10, 28, 3, -30)
draw_poly_num(28, 10, 28, 3, -240)
hideturtle()
done()
from turtle import *
# 本题中的代码是拷贝了我第5章最后一题的代码段。
# 我再次想问候作者大脑和家人,你真闲点。有机会一定亲自去扇一下yourself。
# 本题和上一个题出现了一样的问题。但是我解决了。同时我也发现了。当然对于大家来说这个问题是小问题。
def drawChessboard(dis_x, dis_y):
speed(230)
penup()
right(45)
penup()
num_input = dis_x
for i in range(4):
dis_x1 = dis_x
dis_y -= num_input
for k in range(4):
goto(dis_x1, dis_y)
pendown()
circle(num_input*0.7, steps=4)
penup()
dis_x1 += num_input
goto(dis_x1, dis_y)
pendown()
begin_fill()
circle(num_input*0.7, steps=4)
end_fill()
penup()
dis_x1 += num_input
dis_x1 = num_input
dis_y -= num_input
for j in range(4):
goto(dis_x1, dis_y)
pendown()
begin_fill()
circle(num_input*0.7, steps=4)
end_fill()
penup()
dis_x1 += num_input
goto(dis_x1, dis_y)
pendown()
circle(num_input*0.7, steps=4)
penup()
dis_x1 += num_input
left(45)
drawChessboard(-28, 0)
drawChessboard(28, 224)
hideturtle()
done()
# 这段代码把我带回了第3章,让我明白了第3章中format的作用。
# 写到这里第一阶段结束了
def format_value(number, width_num):
a = str(number)
return format(number, f">0{width_num}")
# return '{:0>8}'.format(a)
def main():
print(format_value(34, 1))
main()
第一阶段结束了。有时间我再分享前5章的代码吧。
支持我三连哦。