python中的球体积

时间:2017-05-02 03:16:10

标签: python ipython

我可以使用此代码轻松计算python中球体的体积。

import math

radius = input("Enter Radius: ")
print("Radius: " + str(radius))

r = float(radius)

volume = 4.0/3.0 * math.pi * (r*r*r)
print("Volume: " + str(round(volume,2)))

现在我的计划是找到n维的体积。我找到的等式的体积和图形变化是enter image description here

我想像这样使用

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = math.pi^(n/2)/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,2)))

它不起作用。你能帮我找到不同维度的体积,并得到球体积的图表吗?

2 个答案:

答案 0 :(得分:3)

你必须改变:

localhost/student/edit/1

为:

volume = math.pi(n/2)^2/math.gamma(n/2 + 1)

完整代码:

volume = math.pi**(n/2)/math.gamma(n/2 + 1)

输入:

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = math.pi**(n/2)/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,4)))

输出:

Enter dimension: 3

其他:

dimension: 3
Volume: 4.1888

enter image description here

答案 1 :(得分:0)

看起来pow()就是您所需要的:

import math

dimension = input("Enter dimension: ")
print("dimension: " + str(dimension))
n = float(dimension)
volume = pow(math.pi,(n/2))/math.gamma(n/2 + 1)
print("Volume: " + str(round(volume,2)))

pow(x,y)将x返回到幂y。