简体   繁体   中英

The volume of a sphere?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM