试图编写一个面向对象的 Python 程序来计算圆柱体的体积
Tried to write an Object Oriented Python program to calculate volume of cylinder
我试图编写一个 OOPS python 程序来计算圆柱体的体积,但是当我 运行 我的代码时,我得到“进程返回 0”。我需要定义 class “Cylinder”,接受用户的输入。下面是我的代码。我做错了什么?
from math import pi
# FUNCTIONS
def cylinder_volume(radius, height):
return ((pi * (radius ** 2))*height)
# main
def Cylinder():
radius = float(input("Radius="))
height = float(input("Height="))
print("Cylinder volume: %d" % (cylinder_volume(radius, height)))
# PROGRAM RUN
if __name__ == "__Cylinder__":
Cylinder()
以下是您如何以面向对象的方式执行此操作。您定义一个 Cylinder 对象,它拥有半径和高度。 I/O; 取决于调用者。该对象仅保存状态并具有方法。然后,对象有一个 volume
方法,即 returns 圆柱体积。
当您 运行 一个 Python 脚本时,__name__
变量总是 "__main__"
。
import math
class Cylinder():
def __init__(self, radius, height):
self.radius = radius
self.height = height
def getVolume(self):
return math.pi * (self.radius ** 2) * self.height
if __name__ == "__main__":
radius = float(input("Radius="))
height = float(input("Height="))
cyl = Cylinder(radius, height)
print("Cylinder volume:", cyl.getVolume())
我试图编写一个 OOPS python 程序来计算圆柱体的体积,但是当我 运行 我的代码时,我得到“进程返回 0”。我需要定义 class “Cylinder”,接受用户的输入。下面是我的代码。我做错了什么?
from math import pi
# FUNCTIONS
def cylinder_volume(radius, height):
return ((pi * (radius ** 2))*height)
# main
def Cylinder():
radius = float(input("Radius="))
height = float(input("Height="))
print("Cylinder volume: %d" % (cylinder_volume(radius, height)))
# PROGRAM RUN
if __name__ == "__Cylinder__":
Cylinder()
以下是您如何以面向对象的方式执行此操作。您定义一个 Cylinder 对象,它拥有半径和高度。 I/O; 取决于调用者。该对象仅保存状态并具有方法。然后,对象有一个 volume
方法,即 returns 圆柱体积。
当您 运行 一个 Python 脚本时,__name__
变量总是 "__main__"
。
import math
class Cylinder():
def __init__(self, radius, height):
self.radius = radius
self.height = height
def getVolume(self):
return math.pi * (self.radius ** 2) * self.height
if __name__ == "__main__":
radius = float(input("Radius="))
height = float(input("Height="))
cyl = Cylinder(radius, height)
print("Cylinder volume:", cyl.getVolume())