《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
  • 15
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值