《python语⾔程序设计基础》嵩天著-第2章程序练习题答案练习题代码
正在看书⾃学中,贴出⾃⼰的作业,欢迎⼤家交流指正,⼀起进步~
2.1 实例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 汇率兑换
136a工字钢是什么意思# Exchange rate conversion
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 实例2的修改
#draw a multicolored python
from turtle import*
setup(500,400,200,200) pencolor('green')
penup()
fd(-250)
pendown()
pensize(20)
seth(-40)
r =0.1
g =0.2
b =0.3
for i in range(4):
pencolor((r+0.1*i, g+0.1*i, b+0.1*i)) circle(40,80)
navicat环境配置circle(-40,80)
pencolor((r+0.1*4, g+0.1*4, b+0.1*4)) circle(40,80/2)
fd(40)
pencolor((r+0.1*5, g+0.1*5, b+0.1*5)) circle(16,180)
fd(40*2/3)
done()
2.4 等边三⾓形
# Equilateral Triangle
from turtle import*
setup(500,400,200,200) pencolor('green')
penup()
fd(-100)
seth(-90)
fd(50)
pendown()
pensize(5)
for theta in[0,120,-120]:
seth(theta)
fd(200)
done()
2.5 叠加等边三⾓形
#叠加等边三⾓形的绘制
len=40
#ang = 60
from turtle import*
setup()
seth(0)
fd(len)
seth(120)
fd(len)
seth(240)
fd(2*len)
seth(0)
fd(2*len)
seth(120)
fd(len)
seth(240)
fd(len)
seth(120)
css设置列表样式fd(len)
done()
2.6 ⽆⾓正⽅形
# Square without corners from turtle import* setup(500,400,200,200) pencolor('green')
penup()
fd(-100)
seth(-90)
switch case支持哪些数据类型fd(60)
for i in[0,90,180,270]:
fd(40)
seth(i)
fd(40)
pendown()
fd(120)
penup()
done()
2.7 六⾓形
#绘制六⾓形
from turtle import *
len = 180
for i in [0,1]:
seth(30+60*i)
fd(len)
seth(270+60*i)
fd(len)
seth(150+60*i)
fd(len)
penup()
python基础代码练习seth(-30)
fd(len/3)
seth(-90)
fd(len/3)
pendown()
done()
2.8 正⽅形螺旋线
# 绘制正⽅形螺旋线from turtle import* len=500
setup()
penup()
seth(180)
fd(250)
seth(270)
fd(250) pendown()
speed(10)
for i in range(1,15): seth(90)
fd(len)
seth(0)
len=len-9
fd(len)
seth(-90)
gettickcount linuxlen=len-9
fd(len)
seth(180)
len=len-9
fd(len)
len=len-9 done()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论