I'm trying to obtain an instance variable from an instantiated class. The class I've created represents a Stamp.
In this case I'm trying to access the self.cost instance variable as a float in the global environment. To paint a bit more of the picture, my program will generate multiple 'Stamp' classes to add to a shopping cart.
I want to be able to total their respective prices to give a total cost amount. I've tried to only present the relevant bits of code below.
The error I'm receiving is - TypeError: unsupported operand type(s) for +=: 'int' and 'method'
The error is obviously occurring at total cost += cost
, although I don't understand why the program is reading the cost as a method and not a float.
See relevant code form within the class below:
self.quantity = int(input("How many of this item would you like to post?))
self.cost = self.price * self.quantity
def get_cost(self):
return float(self.cost)
And code for the global environment:
stamp = Stamp() #instantiate a stamp class
totalCost = 0 #create a variable for the totalCost
cost = stamp.get_cost #create variable to obtain stamp cost from within class
totalCost += cost #each time a class is instantiated this will add the cost