Forums

Variable Problems

Code:

class ListItem(object):
        tools_list = {}
        materials_list = {}
        def __init__(self, name, cost, typ):
            self.name = name
            self.cost = cost
            self.typ = typ
        def add_item(self):
                if self.typ == "tool":
                    if not self.name in self.tools_list:
                        self.tools_list[self.name] = self.cost
                        print (self.tools_list)
                    else:
                        print ("already added to tools list")
                elif self.typ == "material":
                    if not self.name in self.materials_list:
                        self.materials_list[self.name] = self.cost
                        print (self.materials_list)
                    else:
                        print ("already added to materials list")



    def create_item():
        a = input("name of product: ")
        b = float(input("cost of product: "))
        c = input("type of product: ")
        item = ListItem(a, b, c)
        item.add_item()

    def calculate_total():
        tools_list = ListItem()
        materials_list = ListItem()
        of_what = input("which cost total do you want? (tools, materials, or both) ")
        if of_what == "tools":
            count = 0
            for i in tools_list:
                count = count + tools_list[i]
                print (count)
                return count
        elif of_what == "materials":
            count = 0
            for i in materials_list:
                count = count + materials_list[i]
                print (count)
                return count
        elif of_what == "both":
            count_t = 0
            count_m = 0
            for i in tools_list:
                count_t = count_t + tools_list[i]
                return count
            for i in materials_list:
                count_m = count_m + materials_list[i]
                return count_m
            print (count_m + count_t)
            return (count_m + count_t)
        else:
            print("please try again")

Hey! This is the first time I've tried to code anything individually, so I'm sorry if my code is a mess. I want to be able to come up with a way to add the values stored in tools_list and materials_list (local variables in the ListItem class). The calculate_total function was my attempt at this (I know the tools_list = ListItem() was a stupid idea), but I can't figure out a way to get those two dictionaries out of the class and into the function without disrupting the rest of the code. Any ideas? Thanks!

[edit by admin: formatting]

Fixed it!