从零开始学Python

Python习题册015:计算半径为6的球体体积

2019-01-17  本文已影响0人  iLester

任务011描述

用Python编写一个程序,计算半径为6的球体的体积。
已知球体的体积公式为: V = 4/3 × π × r^3 = π × d^3/6。

分析及示例

Python : Volume of a sphere

既然已经知道球体的体积公式了,所以要做的就是定义变量,构建表达式并输出。这里pi常量可以自行定义,也可以通过math.pi来获取。

import math

radius = 6
V = 4/3 * math.pi * radius**3

print('The volume of the sphere is {:.2f}'.format(V))

运行结果:

The volume of the sphere is 904.78
上一篇 下一篇

猜你喜欢

热点阅读