PDF p61, Book p45
编程题2.1温度转换
celsius = eval(input("请输入摄氏温度="))
fahreheit = (9 / 5) * celsius + 32
print("转换后的华氏温度=", fahreheit)
编程题2.2计算圆柱体的体积
radius = 20
length = 30
PI = 3.14159
area = radius * radius * PI
volume = area * length
print(area)
print(volume)
编程题2.3英尺转化为米
feets = eval(input("请输入英尺="))
meters = feets * 0.305
print(feets, " 英尺=", meters, " 米")
编程题2.4磅转化为千克
pounds = eval(input("请输入磅="))
kilograms = pounds * 0.454
print(pounds, " 磅 =", kilograms, " 千克")
编程题2.5计算小费
subtotal, gratuityRate = eval(
input("Please input subtotal and gratuityRate:")) # 输入:小计、酬金率
gratuity = subtotal * gratuityRate/100
total = subtotal * (1 + gratuityRate/100)
print(
"subtotal:", # 小计
subtotal,
" gratuityRate:", # 酬金率
gratuityRate,
" gratuity:", # 酬金
gratuity,
"total:", # 总金额
total)
编程题2.6整数拆解并求和
num = eval(input("请输入0-1000之间的整数:"))
# 9521
sum = num // 100 + (num % 100) // 10 + num % 10
print("sum=", sum)
编程题2.6整数拆解并求和进阶版
input_number = eval(input("请输入一个正整数:"))
input_number = str(input_number) # 必须先转化为字符串形式
sum = 0
for digit in input_number: # digit在字符串中
sum += int(digit)
print(sum)
编程题2.7计算年数和天数
minutes = eval(input("请输入分钟数,例如 10000000 :"))
hours = minutes // 60
days = hours // 24
years = days // 365
remainDays = days % 365
print("转化后,显示为", years, " 年,", remainDays, " 天")
编程题2.8计算能量
# 输入 水的重量
waterWeight = eval(input("请输入水的重量/千克:"))
# 输入 水的初始温度
waterInitalTemp = eval(input("请输入水的初始温度/摄氏度:"))
# 输入 水加热后的温度
waterFinalTemp = eval(input("请输入水加热后的温度/摄氏度:"))
# 计算能量
Q = waterWeight * (waterFinalTemp - waterInitalTemp) * 4184
# 输出结果
print("按输入要求计算,加热所需能量为:", Q, " 焦耳")
编程题2.9风寒计算
tA = eval(input("请输入室外华氏温度,低于-58 或 高于 41: "))
if tA <= -58 and tA >= 41:
print("ERROR:室外华氏温度 tA 输入错误,请重新输入!")
exit() # 报错,并退出运行
v = eval(input("请输入高于 2 里/小时的室外风速: "))
if v < 2:
print("ERROR:室外风速输入错误,请重新输入!")
exit() # 报错,并退出运行
tWc = 35.74 + 0.6215 * tA - 35.75 * v**0.16 + 0.4275 * tA * v**0.16
print("当前风寒温度 = {:5f}".format(float(tWc))) # 将 tWc 参数传入{}
编程题2.10计算跑道长度
v = eval(input("请输入飞机的起飞速度,默认单位(米/秒) :"))
a = eval(input("请输入飞机的加速度,默认单位(米/秒^2) :"))
length = v**2 / (2 * a)
print("根据给定的参数,飞机起飞所需的最短跑道长度为 {} 米".format(length))
编程题2.11投资额
finalMoney = eval(input("请用户输入最终金额/元: "))
rateYear = eval(input("请用户输入年利率/%: "))
years = eval(input("请用户输入年数/年:"))
rateMonth = (rateYear / 12) / 100
months = years * 12
initialMoney = finalMoney / ((1 + rateMonth)**months) # initialMoney 初始金额
print("初始投资金额 = ", initialMoney)
编程题2.12打印表格
print("{:<6}".format("a"), "{:<6}".format("b"), "{:<6}".format("a ** b"))
print("{:<6}".format(1), "{:<6}".format("2"), "{:<6}".format("1"))
print("{:<6}".format(2), "{:<6}".format("3"), "{:<6}".format("8"))
print("{:<6}".format(3), "{:<6}".format("4"), "{:<6}".format("81"))
print("{:<6}".format(4), "{:<6}".format("5"), "{:<6}".format("1024"))
print("{:<6}".format(5), "{:<6}".format("6"), "{:<6}".format("15625"))
编程题2.13分隔数字并反向逐行显示
x = eval(input("请输入4位整数:"))
y = str(x) # 必须先转为 str 类型
print(y[::-1])
'''
本题答案运用 "切片+print" 的方法
参考:
range(stop)
举例,list=(range(10)),输出: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(start, stop[, step])
举例,list(range(0, 30, 5)),输出:[0, 5, 10, 15, 20, 25]
解释 s[i:j:k] 是,从i 到j,步长 k,- 表示倒叙
'''
# 笔记参考1:https://blog.csdn.net/evan123mg/article/details/49232089
# 笔记参考2:https://www.cnblogs.com/ifantastic/archive/2013/04/15/3021845.html
编程题2.14三角形的面积
x1, y1 = eval(input("请输入 A 点的坐标 x1,y1 : "))
x2, y2 = eval(input("请输入 B 点的坐标 x2,y2 : "))
x3, y3 = eval(input("请输入 C 点的坐标 x3,y3 : "))
side1 = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
side2 = ((x3 - x2)**2 + (y3 - y2)**2)**0.5
side3 = ((x1 - x3)**2 + (y1 - y3)**2)**0.5
s = (side1 + side2 + side3) / 2
area = (s * (s - side1) * (s - side2) * (s - side3))**0.5 # 面积计算公式
print("三条边的长度分别为:{:.2f} {:.2f} {:.2f}".format(side1, side2, side3))
print("三角形的面积为:{:.2f}".format(area))
# 三角形面积在线计算器:https://zh.numberempire.com/arbitrary_triangle_calculator.php
编程题2.15正六边形的面积
s = eval(input("请输入正六边形的边长:"))
area = (s**2) * 3 * (3**0.5) / 2
print("输入的边长 = {:.2f},正六边形的面积 = {:2f}".format(s, area))
编程题2.16加速度
v0, v1, t = eval(input("请分别输入初始速度(米/秒)、末速度(米/秒)、时间(秒):"))
a = (v1 - v0) / t
print("平均加速度为: {:.2f} 米/秒^2".format(a))
编程题2.17计算BMI
# inche 英尺,pound 磅
height_inches, weight_pounds = eval(input("请分别输入身高(英尺)和体重(磅):"))
BMI = weight_pounds * 0.45359237 / ((height_inches * 0.0254)**2)
print("身高{:.2f}英尺,体重{:.2f}磅,BMI={:.4f}".format(
height_inches, weight_pounds, BMI))
编程题2.19计算未来投资额
amount, rate, years = eval(input("请分别输入投资金额/元,年化利率/%,投资期限/年:"))
accumulateAmount = amount * (1 + rate / 100 / 12)**(years * 12)
print("未来投资总额 = {:.2f} 元".format(accumulateAmount))
编程题2.20计算利息
balance, rate_interest = eval(input("请分别输入差额(元)和年化利息(%):"))
interest = balance * (rate_interest / 1200)
print("The interest is {:.2f} ".format(interest))
编程题2.21复利值
manthly_saving_amount = eval(input("请输入每月存款金额/元:"))
i = 6
rate_month = 1 + 0.00417
sum = 0
for x in range(i):
sum = (manthly_saving_amount + sum) * rate_month
print(sum)
编程题2.23绘制四个圆
import turtle
radius = eval(input("请输入圆的半径:"))
turtle.pensize(2) # 设定画笔粗细
turtle.circle(radius) # 第一个圆圈
turtle.penup() # 第二个圆圈
turtle.goto(radius * (-2), 0)
turtle.pendown()
turtle.circle(radius)
turtle.penup() # 第三个圆圈
turtle.goto(radius * (-2), radius * (-2))
turtle.pendown()
turtle.circle(radius)
turtle.penup() # 第四个圆圈
turtle.goto(0, radius * (-2))
turtle.pendown()
turtle.circle(radius)
turtle.penup()
turtle.goto(radius, 0)
turtle.pendown()
turtle.hideturtle()
turtle.write("半径 = {:.0f} ".format(radius), font=("Arial", 16, "normal"))
turtle.done()
编程题2.23绘制四个圆1
import turtle
turtle.screensize(500, 500)
turtle.pensize(2)
turtle.pencolor("red")
radius = eval(input("请输入圆的半径:"))
turtle.penup() # 第一个圆
turtle.forward(radius)
turtle.pendown()
turtle.circle(radius)
turtle.penup() # 第二个圆
turtle.forward(radius * (-2))
turtle.pendown()
turtle.circle(radius)
turtle.penup() # 第三个圆
turtle.right(90)
turtle.forward(radius * (2))
turtle.pendown()
turtle.right(-90)
turtle.circle(radius)
turtle.penup() # 第四个圆
turtle.forward(radius * 2)
turtle.down()
turtle.circle(radius)
turtle.penup() # 文字
turtle.goto(radius, radius)
turtle.down()
turtle.write("半径 = {:.0f}".format(radius), font=("Arial", 16, "normal"))
turtle.hideturtle()
turtle.done()
编程题2.24绘制4个正六边形
import turtle
turtle.screensize(500, 500)
turtle.pensize(2)
turtle.pencolor("red")
radius = eval(input("请输入圆的半径:"))
turtle.penup() # 第一个圆
turtle.forward(radius)
turtle.pendown()
turtle.circle(radius,360,6)
turtle.penup() # 第二个圆
turtle.forward(radius * (-2))
turtle.pendown()
turtle.circle(radius,360,6)
turtle.penup() # 第三个圆
turtle.right(90)
turtle.forward(radius * (2))
turtle.pendown()
turtle.right(-90)
turtle.circle(radius,360,6)
turtle.penup() # 第四个圆
turtle.forward(radius * 2)
turtle.down()
turtle.circle(radius,360,6)
turtle.penup() # 文字
turtle.goto(radius, radius)
turtle.down()
turtle.write("正六边形外切圆的半径 = {:.0f}".format(radius), font=("Arial", 16, "normal")) # ...半径也可用int(radius)
turtle.hideturtle()
turtle.done()
编程题2.25绘制一个矩形
import turtle
x, y = eval(input("请输入矩形的中心点:"))
a, b = eval(input("请输入矩形的长和宽:"))
# 显示原点坐标
turtle.dot(5, "red")
turtle.write(
"原点 ({},{})".format(
0, 0), align="center", font=(
"Arial", 12, "normal"))
# 显示矩形中心点坐标
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot(5, "blue")
turtle.write(
"矩形中心点 ({},{})".format(
x, y), align="center", font=(
"Arial", 12, "normal"))
# 绘制矩形
turtle.penup()
turtle.goto(x + a / 2, y + b / 2)
turtle.pendown()
turtle.fd(-a)
turtle.right(90)
turtle.fd(b)
turtle.right(-90)
turtle.fd(a)
turtle.right(-90)
turtle.fd(b)
# 隐藏小海龟
turtle.hideturtle()
turtle.done()
编程题2.26绘制一个圆(略作修改)
import turtle
x, y = eval(input("请输入圆心坐标(x,y):"))
radius = eval(input("请输入圆的半径:"))
# 原点坐标
turtle.dot(3, "red")
turtle.write("原点 (0,0)", align="center", font=("Arial", 12, "normal"))
# 圆心坐标
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.dot(3, "blue")
turtle.write(
"圆心 ({:.2f},{:.2f})".format(
x, y), align="center", font=(
"Arial", 12, "normal"))
# 绘制圆形
turtle.pensize(2)
turtle.pencolor("blue")
turtle.right(90)
turtle.penup()
turtle.fd(radius)
turtle.pendown()
turtle.right(-90)
turtle.circle(radius)
# 计算面积
PI = 3.1415927
s = PI * radius * radius
turtle.penup()
turtle.goto(x, -y / 4)
turtle.pendown()
turtle.write(
("r = {:.2f}\ns = {:.2f}").format(
radius, s), align="right", font=(
"Arial", 12, "normal"))
turtle.hideturtle()
turtle.done()
编程题2.18待更新,PDF p63