Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The volume of a sphere?

Tags:

python

The volume of a sphere with radius r is 4/3 π r3. What is the volume of a sphere with radius 5? Hint: 392.6 is wrong!

print (((4/3.0) * 3.14) * 5**3)

It gave me this

523.333333333

what's wrong???

python 2.7

like image 531
Bassam Badr Avatar asked Nov 18 '12 04:11

Bassam Badr


People also ask

Why is the volume of a sphere 4 3?

Volume of a sphere = 4/3 πr3 The difference between the two shapes is that a circle is a two-dimensional shape and a sphere is a three-dimensional shape which is the reason that we can measure the Volume and area of a Sphere.

What is the volume of a 14 in sphere?

1 Answer. The volume of a sphere is 1436.76 cubic inches.


1 Answers

Nothing is wrong, you have the correct answer. You may want to use math.pi instead of 3.14 however to increase precision of your answer.

>>> from __future__ import division # not necessary if using python 3
>>> 4/3*math.pi*5**3
523.59877559829886

See the solution at wolfram alpha

If you use floor division instead of true division you will get 392.6, which is what the hint was getting at:

>>> 4//3*math.pi*5**3
392.69908169872411
like image 82
Matt Avatar answered Oct 02 '22 10:10

Matt