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
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
For those working with version 3:
Simple print statement works as a charm.
print((4/3)*(22/7)*5**3)
output : 523.8095238095237
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
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
For those working with version 3:
Simple print statement works as a charm.
print((4/3)*(22/7)*5**3)
output : 523.8095238095237