这是第二章的习题解答,可能题目的序号有一点错位,改天再修改吧
# 2.1
from __future__ import division
Celsius =eval(raw_input("Enter a degree in Celsius: "))
fahrenheit = (9/5)*Celsius+32
print Celsius , "Celsius is", fahrenheit ,"Fahrenheit"
# 2.2
import math
from __future__ import division
radius,length = eval(raw_input("Enter the radius and length of a sylinder:"))
area = radius*radius*math.pi
volume = area*length
print "The area is",area
print "The volume is",volume
# 2.3
from __future__ import division
mile = eval(raw_input("Enter the value for feet:"))
meter = mile*0.305
# print mile,"feet is",meter,"meters"
print " %.2f feet is %.2f meters" % (mile,meter)
# 2.4
from __future__ import division
p = eval(raw_input("Enter a value in pounds: "))
k =p*0.454
print "%.3f pounds is %.3f kilograms" % (p,k)
# 2.5
from __future__ import division
salary,rate = eval(raw_input("Enter the subtotal and a gratuity rate: "))
gratuity = salary*rate/100
total = gratuity+salary
print "The gratuity is %.3f and the total is %.3f" % (gratuity,total)
# 2.6
from __future__ import division
value = eval(raw_input("Enter a number between 0 and 1000:"))
if 100<=value and value<1000:
sum = value%10+value//10%10+value//100
elif 10<=value and value<100:
sum = value%10+value//10%10
elif value<10:
sum = value
print sum
# 2.7
from __future__ import division
minutes = eval(raw_input("Enter the number of minutes: "))
year =minutes/(24*60)//365
days =minutes/(24*60)%365
print "%d minutes is approximately %d years and %d" % (minutes, year, days)
# 2.8
from __future__ import division
M = eval(raw_input("Enter the amount of water in kilograms: "))
initial_temperature = eval(raw_input("Enter the initial temperature: "))
final_temperature = eval(raw_input("Enter the final temperature: "))
Q = M*(final_temperature-initial_temperature)*4184
print "the energy needed is %.2f" % Q
# 2.9
from __future__ import division
ta = eval(raw_input("Enter the temperature in Farenheit between -58 ans 41: "))
v = eval(raw_input("Enter the wind speed in miles per hour: "))
t_mc = 35.74+0.6215*ta-35.75*(v**0.16)+0.4275*ta*(v**0.16)
print "the wind chill index is %.5f" % t_mc
# 2.10
from __future__ import division
speed ,acceleration = eval(raw_input("Enter the speed and acceleration:"))
length = speed**3/(2*acceleration)
print "The minimun runway length for this airplane is %.3f" % length
# 2.11
from __future__ import division
final_value = eval(raw_input("Enter the account value:"))
rate = eval(raw_input("Enter anual interest rate in percent:"))
nyear = eval(raw_input("Enter number of years: "))
pv = final_value/((1+rate/(12*100))**(nyear*12))
print "initial deposit value is %.10f" % pv
# 2.12
print "a",""*5,"b",""*5,"a**b"
for i in range(1,6):
print i,""*5,i+1,""*5,i**(i+1)
# 2.13
from __future__ import division
import math
x1, y1, x2, y2, x3, y3 = eval(raw_input("Enter three points for a triangle: "))
side1 = math.sqrt((y2-y1)**2+(x2-x1)**2)
side2 = math.sqrt((y2-y3)**2+(x2-x3)**2)
side3 = math.sqrt((y3-y1)**2+(x3-x1)**2)
s = (side1+side2+side3)/2
area = math.sqrt(s*(s-side1)*(s-side2)*(s-side3))
print "The area of the triangle is %.4f : " % area
# 2.14
from __future__ import division
import math
s = eval(raw_input("Enter the side : "))
area = 3*math.sqrt(3)/2*(s**2)
print "The area of the hexagon is %.4f : " % area
# 2.15
from __future__ import division
import math
v0 , v1 , t = eval(raw_input("Enter the v0 , v1 and t : "))
a = (v1-v0)/t
print "The averge acceleration is : %.4f " % a
# 2.16
from __future__ import division
import math
weight = eval(raw_input("Enter weight in pounds : "))
height = eval(raw_input("Enter height in inches :"))
weight_k = weight*0.45359237
height_m = height*0.0254
BMI = (weight_k/height_m)**2
print " BMI is %.2f " % BMI
# 2.17
from __future__ import division
import math
import time
time_zone = eval(raw_input("Enter the time zone offsets to GMT : "))
current_time = time.time()
time_zone1 = int(current_time)-time_zone*3600
s = time_zone1%60
m = (time_zone1//60)%60
h =(time_zone1//3600)%60
print "The current time is %d:%d:%d " % (h,m,s)
# 2.18
from __future__ import division
import math
pv = eval(raw_input("Enter investment amount: "))
rate = eval(raw_input("Enter anual inerest rate : "))
years = eval(raw_input("Enter number of years :"))
av = pv*(1+rate/(12*100))**(years*12)
print "Accmulated value is %.2f" % av
# 2.19
from __future__ import division
balance,rate = eval(raw_input("Enter balance and interest rate( e.g.,3%):"))
interest = balance*(rate/1200)
print "The interest is %.5f" % interest
# 2.20
from __future__ import division
a0 = eval(raw_input("Enter the monthly saving amount : "))
sum= 0
for i in range(1,7):
sum +=a0*(1+0.05/12)**i
print "After the sixth month, the account value is %.2f" % sum
# 2.21
from __future__ import division
years = eval(raw_input("Enter the number of years : "))
def predictPop(year):
a0 = 3120324986
s = year*365*3600
predict_pop = a0+(s/7.0+s/45.0-s/13.0)
return predict_pop
print predictPop(years)
# 2.22
import turtle
turtle.penup()
turtle.goto(-50,-50)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.goto(-50,50)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.goto(50,50)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.goto(50,-50)
turtle.pendown()
turtle.circle(50)
# 2.23
import turtle
turtle.penup()
turtle.goto(-100,0)
turtle.pendown()
turtle.left(30)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(200)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.penup()
turtle.goto(110,0)
turtle.pendown()
turtle.left(0)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(100)
turtle.left(60)
turtle.forward(200)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
turtle.right(60)
turtle.forward(100)
# 2.24
import turtle
length = eval(raw_input("The length is : "))
width = eval(raw_input("The width is : "))
center = eval(raw_input("The height is : (x1,y1)"))
turtle.penup()
turtle.goto(center)
turtle.pendown()
turtle.forward(length)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(width)
# 2.25
import turtle
import math
center = eval(raw_input("The center is :"))
raduis = eval(raw_input("The raduis is :"))
area = math.pi*raduis*raduis
turtle.penup()
turtle.goto(center)
turtle.pendown()
turtle.circle(100)
turtle.penup()
turtle.goto(50,50)
turtle.pendown()
turtle.write("The radius is %.2f " % raduis )
turtle.penup()
turtle.goto(30,50)
turtle.pendown()
turtle.write("The area is %.2f " % area)
turtle.done()