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

2.1

1443552-20180901101006426-516564261.png

程序:

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

1443552-20180901101848775-1506872807.png

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

1443552-20180901103804116-23026086.png

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

1443552-20180901105957711-1225519571.png

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

1443552-20180901110613569-1758102683.png

2.5

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

1443552-20180901113646094-1222638044.png

程序:

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

1443552-20180901113759393-1815173733.png

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

1443552-20180901121358807-714249901.png

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

答案略

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值