第二章 python 程序实例解析
2.1温度转换
Tempstr=eval(input("请输入温度值(无单位):"))
Unit=input("请输入单位:")
if Unit in ['F','f']:
C=(Tempstr-32)/1.8
print("转换后的温度是{:.0f}c63".format(C))
elif Unit in ['c','C']:
F=1.8*Tempstr+32
print("转换后的温度是{:.0f}F".format(F))
2.2.人民币与美元的转换
print("请输入货币:")
Currency=input()
print("请输入类型(人民币为RMB美元为USD):")
type=input("")
if type[1] in ["U"]:
Currency = eval(Currency) /6
print("换算为:{:.2f}RMB".format(Currency))
else:
Currency =eval(Currency) *6
print("换算为:{:.2f}USD".format(Currency))
2.3彩色蟒蛇
代码如下:
import turtle
a=['red','yellow','black','purple']
turtle.setup(650,350)
turtle.penup()
turtle.fd(-250)
turtle .pendown()
turtle .pensize(25)
turtle .seth(-40)
for i in range(4):
turtle .pencolor(a[i])
turtle .circle(40,80)
turtle.circle(-40,80)
turtle.circle(40, 40)
turtle .fd(40)
turtle.circle(16,180)
turtle .fd (40*2/3)
2.4 等边三角形的绘制
代码如下:
import turtle
turtle.pencolor("blue")
turtle.setup(800,600)
turtle.pensize(5)
turtle.fd(300)
turtle.seth(120)
turtle.fd(300)
turtle.seth(240)
turtle.fd (300)
2.5叠加等边三角形
代码如下:
import turtle as t
t.pencolor("blue")
t.fd(200)
t.seth(120)
t.fd(200)
t.seth(-120)
t.fd(200)
t.seth(0)
t.fd(100)
t.seth(60)
t.fd(100)
t.seth(180)
t.fd(100)
t.seth(-60)
t.fd(100)
t.seth(120)
t.fd(100)
t.seth(0)
2.6无角正方形
代码如下:
import turtle
turtle.setup(700,700)
turtle.seth (270)
turtle.penup()
turtle.fd(50)
turtle.pendown()
turtle.fd (100)
turtle.penup()
turtle.fd(50)
turtle.seth(0)
turtle.fd(50)
turtle.pendown()
turtle.fd (100)
turtle.penup()
turtle.fd(50)
turtle.seth (90)
turtle.fd(50)
turtle.pendown()
turtle.fd (100)
turtle.penup()
turtle.fd(50)
turtle.seth (180)
turtle.fd(50)
turtle.pendown()
turtle.fd (100)
turtle.penup()
turtle.fd(50)
2.7 六边形的绘制效果
代码如下:
import turtle
turtle.setup(650,350,200,200)
turtle.pencolor("blue")
turtle.penup()
turtle.fd(-100)
turtle.pendown()
for i in [270,210,150,90,30,-30]:
turtle.seth(i)
turtle.fd(60)
turtle.seth(i-120)
turtle.fd(60)
turtle.seth(i-240)
turtle.fd(120)
turtle.seth(30)
2.8正方形螺旋线
代码如下:
import turtle
turtle.pencolor("blue")
for i in range(10,500,20):
turtle.seth(90)
turtle.fd(i)
turtle.seth(180)
turtle.fd(i)
turtle.seth(270)
turtle.fd(10+i)
turtle.seth(0)
turtle.fd(i + 10)
turtle.seth(0)
2.9 python蟒蛇设计
代码如下:
import turtle
a=['red','yellow','black','purple','blue']
turtle.setup(650,350)
turtle.penup()
turtle.goto(-200,-30)
turtle.pendown()
turtle.pensize(20)
turtle.seth(35)
for i in range(5):
turtle .pencolor(a[i])
turtle.circle(-30,70)
turtle.circle(30,70)
turtle.circle(30,35)
turtle.pencolor('purple')
turtle.fd(60)
turtle.pencolor('red')
turtle.circle(-30,100)
|