The volume of a sphere?

Question:

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

Asked By: Bassam Badr

||

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
Answered By: Matt

For those working with version 3:
Simple print statement works as a charm.

print((4/3)*(22/7)*5**3)

output : 523.8095238095237

Answered By: Ashish Sharma
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.