how to setup an accumulator variable?

First of all, I am writing a code to print inventory of Ikea from a file that has lines like this, F,301.841.73,9.99,HOLMÖ,Floor lamp – gives a soft mood light,none,75,116,22,2.2

where the third one “9.99” is the price of that furniture item. i have getter and setter for the price variable as well.

def setPrice(self, f_price):
    self.__price = f_price

def getPrice(self):
    return self.__price

there is main class called Furniture and other sub-classes for items like “BED”, “NIGHTSTAND”, “ARMCHAIR” etc..

there is a UNIT test framework for these classes. where it creates a list, them opens the file and using loop , it adds all the variables for furniture item like type, an article number, a price, a Swedish-sounding name, a basic description, and optionally, a colour from Furniture class as well as sub variables from sub class to list.

then, AT last, we need to count Total value of inventory. where I initialized a Total_value variable to 0.

total_value = 0

for furniture_item in inventory:
    print(str(furniture_item)) # print the item
    print("="*30) # print a separator
    # ADD THE PRICE OF THE ITEM TO THE ACCUMULATOR VARIABLE
    total_value += (value)

when I try to get price from getPrice function:

price = Furniture.getPrice
    total_value += price

it gives me error like this:

*****Error***
Traceback (most recent call last):
  File "__tester__.python3", line 212, in <module>
      total_value += price
TypeError: unsupported operand type(s) for +=: 'int' and 'function'**

when i do like this:

Furniture.getPrice = price
    total_value += price

It gives me the value of **Total Inventory Value: 3184.0**. But, the value expected is **Total Inventory Value: 1432.91**, I don’t know what wrong I am doing.

If anyone can suggest to me how to get this expected value, that would be great!!!

Answer

I’m guessing you want

total_price += furniture_item.getPrice()

Leave a Reply

Your email address will not be published. Required fields are marked *