<p>我正在为一个类编写一个程序,在给定用户输入的半径值和高度的情况下,求一个平截头体的体积和表面积。我认为我走上了正确的道路,但我对如何将公式放入python中没有信心。计算表面积的公式为:
<img src="https://i.stack.imgur.com/vobvK.png" alt="Formula"/></p>
<p>这是我的密码:</p>
<pre><code>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()
</code></pre>
<p>如果有人能确认这是用python编写公式的正确方法,我们将不胜感激</p>