Reputation:
I am getting an error that says I am missing 2 required positional arguments: 'height' and 'radius.' I feel like I have tried everything but I know I am missing something small. Any help? Thanks
# 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
I am not sure what I am doing wrong here. Below there is the test program for my code.
##
# 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())
Upvotes: 1
Views: 1505
Reputation: 363314
When you define the initializer like this:
class SodaCan:
def __init__(self, height, radius):
...
You are saying that height and radius are required. They must be specified in order to create a soda can instance.
mySodaCan = SodaCan(height=10, radius=4)
If you want them to be optional, you may specify default values for those arguments when you define the __init__
method. Then when you create an instance, the arguments will take the default values if omitted when creating an instance.
Upvotes: 2
Reputation: 1052
You need the pass the height and radius when you initialize the class. Arguments in the init class mean that they have to be passed when you initialize the class. Something like this would work:
height = 40
radius = 10
a = SodaCan(height, radius)
Upvotes: 2