计算球体的体积和表面积的输出

时间:2019-10-12 18:11:38

标签: python

球体的体积和表面积可以用以下公式计算。创建它作为终端应用程序。为体积写一个函数,为表面积写另一个函数。结果应显示体积和表面积,均四舍五入到小数点后两位。使用Python数学模块中的pi。包括以下doctest。你必须通过 所有测试均获得满分。请密切注意函数的命名方式。他们一定 匹配

卷示例/ doctests:

  • 回合(volume_of_sphere(0),2)
    • 0.0
  • 回合(volume_of_sphere(1),2)
    • 4.19
  • 回合(volume_of_sphere(12.3),2)
    • 7794.78
  • 回合(volume_of_sphere(18.9),2)
    • 28279.65
  • 回合(volume_of_sphere(33.33),2)
    • 155093.84

表面积示例/ doctests:

  • round(surface_area(0),2)
    • 0.0
  • round(surface_area(1),2)
    • 12.57
  • round(surface_area(12.3),2)
    • 1901.17
  • round(surface_area(18.9),2)
    • 13959.84
  • round(surface_area(33.33),2)
    • 155093.84
MY CODE:
''' Python3 program to calculate Volume and
Surface area of Sphere'''
# Importing Math library for value Of PI
import math
pi = math.pi

# Function to calculate Volume of Sphere
def volume(r):
    vol = (4 / 3) * pi * r * r * r
    return vol

# Function To Calculate Surface Area of Sphere
def surfacearea(s):
    sur_ar = 4 * pi * r * r
    return sur_ar

# Driver Code
radius = round(volume(1), 2)
area = round(area(0), 2)
print( "Volume Of Sphere : ", volume(radius) )
print( "Surface Area Of Sphere : ", surfacearea(area) )

2 个答案:

答案 0 :(得分:0)

下面的两个程序将查找具有半径的球体的表面积和体积。这只是一个简单的方法。第一个程序更准确,但两者都能胜任。

@HostListener('input', ['$event'])
onKeyDown(event: KeyboardEvent) {
  const input = event.target as HTMLInputElement;

  const trimmed = input.value.replace(/\s+/g, '').slice(0, input.value.indexOf('/')==-1?4:5);
  if (trimmed.length > 3) {
    return (input.value = `${trimmed.slice(0, 2)}/${trimmed.slice(trimmed.indexOf('/')==-1?2:3)}`);
  }
}

pi=22/7
radian = float(input('Radius of sphere: '))
sur_area = 4 * pi * radian **2
volume = (4/3) * (pi * radian ** 3)
print("Surface Area is: ", sur_area)
print("Volume is: ", volume)

答案 1 :(得分:0)

由于这里没有明显的问题,我假设目标是完成数学功能并通过doctest。话虽如此,否则在数学上不正确的情况下就不可能做到这一点,因为表面积函数的最后两个doctest是错误的,InsertDateTime应该是4488.83而不是13959.84,而round(surface_area(18.9), 2)应该是13934.72而不是155093.84。