# File: 3.1.py
# -*- coding: utf-8 -*-
# Calculation of the surface area and volume of a sphere
import math
def calA_V():
print("This program calculates the surface area and volume of a sphere.")
r = eval(input("The radius is: "))
PI = math.pi
v = 4/3*PI*r**3 #**操作符进行指数运算
s = 4*PI*r**2
print("The surface area is: %.2f\nThe volume is: %.2f"%(s, v))
calA_V()
# File: 3.2.py
# -*- coding: utf-8 -*-
# unit cost of pizza
import math
def unitCost():
print("This program calculates the unit cost of\nthe pizza with given diameter and price.")
print("*"*20)
d = eval(input("The diameter of the pizza(inches): "))
price, currency = input("The price of the pizza: ").split() #从空格处分割字符串
price = eval(price)
Ucost = price / (math.pi*d/4)
print("This pizza cost %.2f %s per suqare inch."%(Ucost, currency))
unitCost()
# File:3.3.py
# -*-coding:utf-8 -*-
#
def Counting():
print("This program caculates the formula weight of carbohydrate.")
h, o, c = eval(input("The number of hydrogens, oxygens and carbons (separate by comma): "))
carb= 1.00794*h + 12.1017*o + 15.9994*c
print("The formula weight of carbohydrate is: ", carb)
Counting()
# File: 3.4.py
# -*- coding : utf-8 -*-
def thunderDis():
print("This program cauculates the distance from a thunder.")
t = eval(input("Enter the time interval between the lighting and thunder: "))
print("The distance from the thunder is: %.5f"%(t*1100/5280), "miles.")
thunderDis()
def coffeePrice():
w = eval(input("The weight of coffee (rating in pounds): "))
price = w*(10.5+1.86) + 1.5
print("The price of this order is %.3f dollars"%price)
coffeePrice()
# Flie: 3.6.py
# -*- coding: utf-8 -*-
def rateNdis(a, b):
import math
x1, y1 = a[0], a[1]
x2, y2 = b[0], b[1]
d = math.sqrt((x1 - x2)**2 + (y1 - y2)**2) #计算两点间距离
if x1 == x2:
r = "infinity" #直线垂直于x轴,斜率无穷大
elif y1 == y2:
r = 0 #直线平行于x轴,斜率为0
else:
r = (y1 - y2)/(x1 - x2)
return r, d
p1 = eval(input("The coordinates of the first point: "))
p2 = eval(input("The coordinates of the second point: "))
r, d = rateNdis(p1, p2) #将函数的返回值赋给r, d
print("The rate of this line is: ",r, "\nThe distance of this segment is: ",d)
运行结果:
# File:3.8.py
# -*- coding:utf-8 -*-
def main():
year = eval(input("Enter the year: "))
c = year//100
epact = (8 + (c//4) - c +((8*c + 13)//25) + 11*(year%19))%30
print("Epact: ",epact)
main()
# Flie:3.9.py
# -*- coding: utf-8 -*-
import math #
def TriArea():
"This program calculates the area of a triangle according the length of its sides."
sides = eval(input("Enter the length of three sides of a triangle: ")) #此时sides为字典类型
s = sum(sides)/2
A = math.sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2]))
print("The area of the triangle is: %.2f"%A)
TriArea()
# File: 3.10.py
# -*- coding: utf-8 -*-
import math #
def main():
h = eval(input("The height of the ladder: "))
a = eval(input("The angle between the latter and the wall: "))
PI = math.pi
print("The length of the ladder should be %.2f"%(h/math.sin(a/180*PI)))
#计算时应将输入的角度转为弧度
main()
# File 3.11.py
# -*- coding: utf-8 -*-
import math #
def main():
n = int(input("The number of the nature number is: "))
a, b = 0, 0
for i in range(n):
a = a + i
b = b + i**2
print("The sum is: ", a)
print("The sum of quadratic sum is: ", b)
main()
# File: 3.14.py
# -*- coding: utf-8 -*-
# average
def main():
n = eval(input("How many figures are there?\n"))
s = 0
for i in range(n):
a = eval(input("Enter the figures: "))
s = s + a
print("The input figures added up to be: ", s)
print("The average value of the input numbers is:", float(s/n))
main()
运行结果:
# File 3.15.py
# -*- coding: utf-8 -*-
import math #
def pi():
p = 0
n = eval(input("The length of the series is: "))
for i in range(n):
p =p + 4/(2*i+1)*(-1)**i
print("The Approximate value of π is: ",p, "\nThe deviation is: ", (math.pi-p))
pi()
运行结果:
# File: 3.16.py
# -*- coding: utf-8 -*-
def main():
"This program calculates the n-th Fibonacci number."
n = int(input("Enter the index: "))
if n > 2:
f = [1, 1]
for i in range(n-2):
f.append(f[-1] + f[-2]) #append方法在列表末尾增添一项,增添项为当前数列最后两项之和
fi = f[-1]
else:
fi = 1
print("The %d-th Fibonacci number is: %d"%(n,fi))
main()
运行结果:
# File:3.17.py
# -*- coding: utf-8 -*-
import math #
def main():
x = float(input("Enter a positive number: "))
n = int(input("Enter the number of iterations: "))
guess = x/2
for i in range(n):
guess = (guess+x/guess)/2 #迭代计算平方根
e = abs(math.sqrt(x) - guess) #计算猜测值与真实值间误差
print("The guess value is: %f\nThe error is: %f"%(guess, e))
if e > 0.001: #如果误差过大,则增加迭代次数
print("You may want to increase the number of iterations to get a more accurate value.")