我正在为一个类编写一个程序,在给定用户输入的半径值和高度的情况下,求一个平截头体的体积和表面积。我认为我走上了正确的道路,但我对如何将公式放入python中没有信心。计算表面积的公式为:
这是我的密码:
import math
def main():
radius1Length = float(input("Please enter the first radius:"))
radius2Length = float(input("Please enter the second radius:"))
heightNum = float(input("Please enter the height:"))
volumeTotal = volume(radius1Length,radius2Length,heightNum)
sAreaTotal = surfaceArea(radius1Length,radius2Length,heightNum)
print("The radius values used were:", radius1Length, "and", radius2Length)
print("The height used was:", heightNum)
print("The volume is:", volumeTotal)
print("The surace area is:", sAreaTotal)
## Compute the volume of a frustum
# @pram radius1 a float giving the length of the first radius value
# @pram radius2 a float giving the length of the second radius value
# @pram height a float giving the height value
# @return the volume of the frustum as a float
def volume(radius1,radius2,height):
volumeValue = (1/3) * math.pi * height * (radius1**2 + radius2**2 + (radius1 * radius2))
return volumeValue
## Compute the surface area of a frustum
# @pram radius1 a float giving the length of the first radius value
# @pram radius2 a float giving the length of the second radius value
# @pram height a flot givign the height value
# @raturn the surface area of the frustum as a float
def surfaceArea(radius1,radius2,height):
sArea = math.pi * ((radius1 + radius2) * math.sqrt( height**2 + ((radius2 - radius1)**2) + (math.pi * (radius1**2))))
return sArea
main()
如果有人能确认这是用python编写公式的正确方法,我们将不胜感激
括号有问题。你写道:
对应于:
正如您所注意到的,
pi * (r1 ** 2)
也放在平方根之下,这是不正确的。你知道吗您可以将其重写为:
或更详细:
但实际上,写
x * x
通常比写x ** 2
更有效(而且数字正确)。你知道吗编辑:
然而,你提出的公式是不正确的。公式为:
我们可以将其实现为:
平截头体的表面积不应该如下所示吗?你知道吗
除了括号的错误位置,你还漏掉了
pi * (radius2**2)
?你知道吗相关问题 更多 >
编程相关推荐