《Python语言程序设计基础》第二版嵩天第二章程序练习题答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《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]))+'¥')
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。