《Python语⾔程序设计基础》第⼆版嵩天第⼆章程序练习题答案2.1
# TempConvert
def TempConvert(value):
if tempstr[-1]in['F','f']:
c =(eval(tempstr[0:-1])-32)/1.8
print("The temperature is converted to {}C".format(int(c)))
elif tempstr[-1]in['C','c']:
f =eval(tempstr[0:-1])*1.8+32
print("The temperature is converted to {}F".format(int(f)))
else:
print("Input wrong!")
tempstr =input("pls input the temperature with unit F or C: ")
while tempstr[-1]not in['N','n']:
TempConvert(tempstr)
tempstr =input("pls input the temperature with unit F or C: ")
2.2汇率兑换程序
while True:
try:
s=input('输⼊⾦额以¥或$结尾,输⼊E结束: ')
mode = s[-1]
if mode=='E':
break
elif mode=='$':
print(str(6*int(s[:-1]))+'¥')
python基础程序设计else:
print(str(round(int(s[:-1])/6,2))+'$')
except:
print("Input Error!")
2.3绘制彩⾊的蟒蛇
# 绘制彩⾊的蟒蛇
import turtle
list=['red','black','grey','gold','purple','violet']
def drawSnake(radius,angle,length):
turtle.seth(-40)
for i in range(length):
turtle.pencolor(list[i%6])
turtle.circle(radius,angle)
turtle.pencolor(list[(i+1)%6])
turtle.circle(-radius,angle)
turtle.circle(radius,angle/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.setup(650,350)
turtle.penup()
turtle.fd(-300)
turtle.pendown()
turtle.pensize(25)
drawSnake(40,80,9)
turtle.done()
2.4等边三⾓形绘制
import turtle
turtle.setup(650,350,200,250) turtle.penup()
turtle.fd(-200)
turtle.pendown()
turtle.pensize(2)
turtle.pencolor('red')
for i in range(3):
turtle.seth(120*i)
turtle.fd(100)
2.5叠加等边三⾓形绘制
list=[60,180,300]
for i in range(3):
turtle.seth(120*i)
turtle.fd(200)
turtle.penup()
turtle.seth(360)
turtle.fd(100)
turtle.pendown()
for i in list:
turtle.seth(i)
turtle.fd(100)
2.6⽆⾓正⽅形绘制
for i in range(1,5):
turtle.fd(100)
turtle.seth(90*i)
turtle.penup()
turtle.fd(10)
turtle.pendown()
2.7六⾓形的绘制
for i in range(3):
turtle.seth(30+120*i)
turtle.fd(180)
turtle.penup()
turtle.seth(360)
turtle.fd(100)
turtle.pendown()
for i in range(3):
turtle.seth(90+120*i)
turtle.fd(180)
2.8正⽅形螺旋形的绘制
len=40
for i in range(1,10):
turtle.seth(90)
turtle.fd(len)
turtle.seth(0)
len=len+9
turtle.fd(len)
turtle.seth(-90)
len=len+9
turtle.fd(len)
turtle.seth(180)
len=len+9
turtle.fd(len)
len=len+9

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。