找到截头台的总表面积,穹窿有问题

2024-06-26 00:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个类编写一个程序,在给定用户输入的半径值和高度的情况下,求一个平截头体的体积和表面积。我认为我走上了正确的道路,但我对如何将公式放入python中没有信心。计算表面积的公式为: Formula

这是我的密码:

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编写公式的正确方法,我们将不胜感激


Tags: ofthevaluemathfloatheightvolumeradius
2条回答

括号有问题。你写道:

sArea = pi * ((r1+r2) * sqrt(h**2 + ((r2-r1)**2) + (pi * (r1**2))))

对应于:

wrong equation

正如您所注意到的,pi * (r1 ** 2)也放在平方根之下,这是不正确的。你知道吗

您可以将其重写为:

from math import pi, sqrt

def surfaceArea(r1, r2, h):
    return pi * (r1 + r2) * sqrt((r2-r1)**2 + h*h) + pi * r1 * r1

或更详细:

from math import pi, sqrt

def surfaceArea(radius1, radius2, h):
    return pi * (radius1 + radius2) * sqrt((radius2-radius1)**2 + height**2) + pi * (radius1**2)

但实际上,写x * x通常比写x ** 2更有效(而且数字正确)。你知道吗

编辑:

然而,你提出的公式是不正确的。公式为:

correct equation

我们可以将其实现为:

from math import pi, sqrt

def surfaceArea(r1, r2, h):
    return pi * ((r1 + r2) * sqrt((r2-r1)**2 + h*h) + r1*r1 + r2*r2)

平截头体的表面积不应该如下所示吗?你知道吗

pi * (radius1 + radius2) * sqrt((radius2-radius1)**2 + height**2)
+ pi * (radius1**2)
+ pi * (radius2**2)

除了括号的错误位置,你还漏掉了pi * (radius2**2)?你知道吗

相关问题 更多 >