some examples.

1、

        Currency exchange program. According to the design idea of ​​the temperature conversion program, write a two-way exchange program between the US dollar and RMB according to
        the exchange rate .

MoneyStr = input("请输入带有符号的金额:")
if MoneyStr[-1] in ['美元','d']:
    C = (eval(MoneyStr[0:-1]) ) * 6
    print("转换为人民币的金额是{:.2f}人民币" .format(C))
elif MoneyStr[-1] in ['人民币','元']:
    F = eval(MoneyStr[0:-1] ) / 6
    print("转换后的温度是{:.2f}美元" .format(F))
else:
    print("输入格式错误")


2.
        Modification of Example 2. Modify the example code 2.1 to draw a colorful python, that is, when drawing
        each small segment of the Python python, the drawing color of the brush will change.
        Tip: put the brush color control function near the python draw function.

#a2.3DrawPython.py
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("green")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.pencolor("purple")
    turtle.circle(-40,80)
    turtle.pencolor("yellow")
turtle.pencolor("black")
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40 * 2/3)


3、

 

        Drawing of an equilateral triangle. Use the turtle.fd0 and turtle.setho functions in the turtle library
        to draw an equilateral triangle, as shown in the figure.

#e2.1DrawTriangle.py
import turtle
turtle.setup(650,550,2,100)
turtle.fd(250)
turtle.penup()
turtle.seth(120)
turtle.pendown()
turtle.fd(250)
turtle.penup()
turtle.seth(-120)
turtle.pendown()
turtle.fd(250)


4.
        The drawing of superimposed equilateral triangles. Use the turtle.fd0 function and turtle.seth0
        function to draw a superimposed equilateral triangle, as shown in the figure.

#e2.5DrawTriangle.py
import turtle
turtle.setup(650,550,2,100)
turtle.fd(200)
turtle.seth(120)
turtle.fd(200)
turtle.seth(-120)
turtle.fd(100)
turtle.seth(0)
turtle.fd(100)
turtle.seth(-120)
turtle.fd(100)
turtle.seth(120)
turtle.fd(100)
turtle.seth(-120)
turtle.fd(100)

 


5、

Drawing of a square without corners. Use the turtle library function to draw a square without corners, as
shown in the figure.

#e2.6DrawSquare.py
import turtle
turtle.setup(600,820,2,100)
turtle.fd(200)
turtle.penup()
turtle.fd(60)
turtle.seth(90)
turtle.fd(60)
turtle.pendown()
turtle.fd(200)

turtle.penup()
turtle.fd(60)
turtle.seth(180)
turtle.fd(60)
turtle.pendown()
turtle.fd(200)

turtle.penup()
turtle.fd(60)
turtle.seth(270)
turtle.fd(60)
turtle.pendown()
turtle.fd(200)


6、

 

Drawing of hexagons. Using the turtle library to draw a hexagon, the effect is shown in the figure.

 

import turtle 
turtle.setup(650,400,200,200)    
turtle.seth(30)                  #画笔的起始方向
for i in range(6):
    turtle.fd(60)                #六角形边长30
    turtle.left(120)             #逆时针移动120度
    turtle.fd(60)
    turtle.left(120)
    turtle.fd(60)
    turtle.left(120)             #画完一个三角形
    turtle.fd(60)
    turtle.right(60)             #转换方向,开始准备新的三角形


7、

Drawing of a square spiral. Use the turtle library to draw a square spiral, the effect is as shown in the figure.

 

import turtle 
turtle.setup(650,400,200,200)    
turtle.seth(90)                  #画笔的起始方向
a=0
for i in range(25):
    a = a+5
    turtle.fd(a)                
    turtle.left(90)             
    turtle.fd(a+5)
    turtle.left(90)
    turtle.fd(a+5)
    turtle.left(90)             
    
    turtle.fd(a+10)
    turtle.left(90)             
    a = a+5

 

Guess you like

Origin blog.csdn.net/fan18317517352/article/details/122906726