《Python语言程序设计基础》第二版 嵩天 第二章程序练习题答案

tech2022-07-13  187

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
最新回复(0)