《python语言程序设计》_第二章编程题

2.1

程序:

Celsius=eval(input("Enter a degree in Celsius:"))
#输入摄氏度的值Celsius
fahrenheit =(9/5)*Celsius + 32
#定义华氏温度fahrenheit
print(Celsius,"Celsius is",fahrenheit,"Fahrenheit")

结果:

Enter a degree in Celsius:43
43 Celsius is 109.4 Fahrenheit

2.2 读取半径和高来计算圆柱体底面面积和体积

设计:

area = radius * radius *∏ 

volume =area * length 

程序:

radius,length = eval (input("Enter the radius and length of cylinder:" ))
#输入半径radius和高length
area = radius*radius*3.14
#定义面积公式
volume = area * length
#定义圆柱体的体积
print("The area is",area)
print("The volume is",volume)

结果:

Enter the radius and length of cylinder:5.5,12
The area is 94.985
The volume is 1139.82

2.3 英尺转米尺,一英尺等于0.305米

程序:

feet = eval(input("Enter a value for feet:"))
meters=feet*0.305
print(feet,"feet is",meters,"meters")

结果:

Enter a value for feet:16.5
16.5 feet is 5.0325 meters

2.4

磅数 转换成千克数,一磅等于0.454千克

 程序:

feet = eval(input("Enter a value for feet:"))
meters=feet*0.305
print(feet,"feet is",meters,"meters")

结果:

Enter a value for feet:16.5
16.5 feet is 5.0325 meters

2.5

编写一个读取小计和酬金然后计算小费以及合计金额的程序,如果用户键入的小计是10,酬金率是15%,程序就会显示是1.5,合计金额是11.5

程序:

subtotal,gratuityRate=eval(input("Enter the subtotal and a gratuity rate:"))
gratuity= subtotal*gratuityRate/100
total=gratuity+subtotal
print("The gratuity is",gratuity,"and the total is",total)

结果:

Enter the subtotal and a gratuity rate:15.69,15
The gratuity is 2.3535 and the total is 18.043499999999998

2.6  读取一个0到1000之间的整数并计算它各位数字之和,例如:如果一个整数是932,那么它各位数字之和就是14。(提示:使用%来提取数字,使用//运算符来去除被提取的数字。例如:932%10=2而932//10=93)

程序:

A=eval(input("Enter a numer between 0 and 1000:"))
b=A%10 #运算符%求余符号,A%100得到的余数是A的个位数,int()函数表示去掉了浮点数,取整
c=A//100 #运算符//是表示整除,得到的相当于A的百位数
d=(A-100*b-c)/10    #或者,(((A-b)/10)%10)  
print("The sum of the digits is",int(b+c+d))

 结果:

Enter a numer between 0 and 1000:999
The sum of the digits is 27

2.7 输入分钟数,计算年数和天数

答案略

猜你喜欢

转载自www.cnblogs.com/qiyuanjiejie/p/9569901.html