用实例编写 Python示例求圆的直径周长和面积。数学公式为:
这个 Python示例允许用户输入圆的半径。通过使用半径值,这个 Python 代码可以找到圆的周长、直径和面积。
提示:参考寻找圆的面积文章,了解寻找蟒圆的面积的各种方法。
# Python Program to find Diameter, Circumference, and Area Of a Circle
PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))
diameter = 2 * radius
circumference = 2 * PI * radius
area = PI * radius * radius
print(" \nDiameter Of a Circle = %.2f" %diameter)
print(" Circumference Of a Circle = %.2f" %circumference)
print(" Area Of a Circle = %.2f" %area)
这个 python 代码查找圆的直径、周长和面积同上。但是,我们使用 Python 函数分离了逻辑。
# Python Program to find Diameter, Circumference, and Area Of a Circle
import math
def find_Diameter(radius):
return 2 * radius
def find_Circumference(radius):
return 2 * math.pi * radius
def find_Area(radius):
return math.pi * radius * radius
r = float(input(' Please Enter the radius of a circle: '))
diameter = find_Diameter(r)
circumference = find_Circumference(r)
area = find_Area(r)
print("\n Diameter Of a Circle = %.2f" %diameter)
print(" Circumference Of a Circle = %.2f" %circumference)
print(" Area Of a Circle = %.2f" %area)
蟒蛇直径和圆周输出
Please Enter the radius of a circle: 12
Diameter Of a Circle = 24.00
Circumference Of a Circle = 75.40
Area Of a Circle = 452.39
标签: 有奖活动