我收到一条错误消息,提示我缺少2个必需的位置参数:“高度”和“半径”。我觉得我已经尝试了一切,但我知道我缺少一些小东西。有什么帮助吗?
谢谢

# import math
import math

class SodaCan :
    # Constructs sodaCan with a given height and radius
    # @param height = given height and radius = given radius
    def __init__(self, height, radius):
        self._height = height
        self._radius = radius

    # Constructs the volume with the given height and radius
    def volume(self):
        self._volume = (pi * (self._radius ** 2) * self._height)

    # Constructs the Surface Area with the given height and radius
    def surfaceArea(self):
        self._surfaceArea = (2 * pi * self._radius * self._height) + (2 * pi   * (self._radius)**2)

    # Return the volume
    def getVolume(self):
        return self._volume

    # Return the Surface Area
    def getSurfaceArea(self):
        return self._surfaceArea


我不确定我在做什么错。下面是我的代码的测试程序。

##
# This program test the sodaCan.py
##

# import math so the program can read pi
import math

# from the file folder, ipmort the code from program 'sodaCan'
from sodaCan import SodaCan

mySodaCan = SodaCan()
mySodaCan.height(10)
mySodaCan.radius(4)

print(mySodaCan.getVolume())
print(mySodaCan.getSurfaceArea())

最佳答案

初始化类时,需要传递高度和半径。 init类中的参数意味着在初始化类时必须将其传递。这样的事情会起作用:

height = 40
radius = 10
a = SodaCan(height, radius)

关于python - builtins.TypeError:__init __()缺少2个必需的位置参数:“height”和“radius”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43103269/

10-12 22:10