python教材第三章答案_《Python语言程序设计》第三章.练习与作业

Python语言程序设计 封面.jpeg

编程题3.1一个五边形的面积

import turtle

import math

r = eval(input("请输入五边形的边长:"))

s = 2 * r * math.sin(math.pi / 5)

area = 5 * s * s / (4 * math.tan(math.pi / 5))

turtle.circle(r, 360, 5)

print("area=", round(area, 2))

turtle.done()

编程题3.2大圆距离

import math

x1, y1 = eval(input("请输入地球上A点的经纬度 x1,y1:"))

x2, y2 = eval(input("请输入地球上B点的经纬度 x2,y2:"))

radius = 6371.01

# 使用math.radians()将度数转化为弧度数

x1 = math.radians(x1)

y1 = math.radians(y1)

x2 = math.radians(x2)

y2 = math.radians(y2)

d = radius * math.acos(math.sin(x1) * math.sin(x2) +

math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))

print("The distance between the two points is {:.2f} km".format(d))

编程题3.3面积估算

'''

地图网站 www.gps-data-team.com/map

68923,Atlanta NE in US,GPS Position: 99°24'18"W, 40°24'37"N for x1,y1

73073,Orlando OK in US,GPS Position: 97°24'29"W, 35°56'40"N for x2,y2

36033,Georgiana AL in US,GPS Position: 86°38'5"W, 31°37'28"N for x3,y3

78.11,Charlotte TX in US,GPS Position: 98°39'20"W, 28°48'51"N for x4,y4

'''

import math

# 经纬度坐标值,转化为数值形式

x1,y1=99.405,40.410

x2,y2=97.408,35.944

x3,y3=86.634,31.624

x4,y4=98.656,28.814

# 经纬度转化为弧度数

x1,y1= math.radians(x1),math.radians(y1)

x2,y2 = math.radians(x2),math.radians(y2)

x3,y3 = math.radians(x3),math.radians(y3)

x4,y4 = math.radians(x4),math.radians(y4)

radius = 6371.01

# 计算城市间距离

dAO=radius * math.acos(math.sin(x1) * math.sin(x2) +

math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))

dAG=radius * math.acos(math.sin(x1) * math.sin(x3) +

math.cos(x1) * math.cos(x3) * math.cos(y1 - y3))

dAC=radius * math.acos(math.sin(x1) * math.sin(x4) +

math.cos(x1) * math.cos(x4) * math.cos(y1 - y4))

dOG=radius * math.acos(math.sin(x3) * math.sin(x2) +

math.cos(x3) * math.cos(x2) * math.cos(y3 - y2))

dCG=radius * math.acos(math.sin(x3) * math.sin(x4) +

math.cos(x3) * math.cos(x4) * math.cos(y3 - y4))

# 计算三角形的面积,ppt p63 面积公式

sAOG=(dAO+dAG+dOG)/2

sACG=(dAC+dAG+dCG)/2

areaAOG=math.sqrt(sAOG*(sAOG-dAG)*(sAOG-dAO)*(sAOG-dOG))

areaACG=math.sqrt(sACG*(sACG-dAG)*(sACG-dAC)*(sACG-dCG))

areaTotal=areaAOG+areaACG

# 输出结果

print("areaTotal = ",areaTotal)

编程题3.4五角形的面积

import math

s = eval(input("请输入五角形的边长 s = "))

area = 5 * math.pow(s, 2) / (4 * math.tan(math.pi / 5)) # 注意 / 符号后面 (),否则计算次序会错

print("area = ", area)

编程题3.5一个正多边形的面积

import math

n, s = eval(input("请分别输入正多边形的边数n,边长s:"))

area = n * math.pow(s, 2) / (4 * math.tan(math.pi / n))

print("边长{},正{}边形的面积是 {:.2f}".format(s,n,area))

编程题3.6找出ASCII码的字符

x = eval(input("请输入0-127的ASCII码值:"))

print(chr(x))

编程题3.7大写的随机字符

# 大写的随机字符,对应的数字65~90

import time

a = int(time.time()) % 26 + 65

b = int(time.time()) % 26 + 97

print(chr(a)) # 随机大写,解题关键是确定 A~Z 的 ASCII 范围

print(chr(b)) # 随机小写

编程题3.9工资表

name = input("Enter emplyee's name:")

hoursWorked = eval(input("Enter number of hours worked in a week:"))

payRate = eval(input("Enter hourly pay rate:"))

ftRate = eval(input("Enter federal tax withholding rate:"))

stRate = eval(input("Enter state tax withholding rate:"))

grossPay = hoursWorked * payRate

federalWithholding = hoursWorked * payRate * ftRate

stateWithholding = hoursWorked * payRate * stRate

totalDeduction = federalWithholding + stateWithholding

netPay = grossPay * (1 - ftRate - stRate)

print()

print(

"Employee Name: {}\nHours Worked: {:.1f}\nPay Rate:${:.1f}\nGross Pay: ${:.1f}".format(

name,

hoursWorked,

payRate,

grossPay))

print()

print(

"Decutions:\n Federal Withholding (20.0%):${:.1f}\n State Withholding (9.0%):${:.2f}\n Total Deduction:${:.2f}".format(

federalWithholding,

stateWithholding,

totalDeduction))

print()

print("Net Pay:$ {:.2f} ".format(netPay))

编程题3.10显示统一码

print("\u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7 \u03b8")

编程题3.11反向数字

num = eval(input("请输入一串整数:"))

print(str(num)[::-1]) # 切片倒序打印

暂未更新3.8,其他暂未更新

你可能感兴趣的:(python教材第三章答案)