Class Initialization Issue

Class Initialization Issue
0

I’m working on an assignment to create a class for homes and an inventory program for it, but I’m struggling with the initialization where I keep getting an error that it’s missing a positional argument. I can’t figure out what I’m doing wrong, can someone please help me?

print("Home Inventory List")

class Homes:

    def __init__(self):
        self.__Modelname = ''
        self.__salestatus = ''
        self.__squarefeet = 0
        self.__address = ''
        self.__city = ''
        self.__state = ''
        self.__zipcode = 0
        self.home_attributes()

    def home_attributes(self):
        try:
            self.__Modelname = input("Enter the model name: \n")
            self.__salestatus = input("Enter the sale status (Sold, Available, Under Contract): \n")
            self.__squarefeet = int(input("Enter the square feet of the home: \n"))
            self.__address = input("Enter the address: \n")
            self.__city = input("Enter the city: \n")
            self.__state = input("Enter the state: \n")
            self.__zipcode = int(input("Enter the zipcode: \n"))
        except ValueError:
            print("You entered invalid information, please try again.")
            self.home_attributes()

    def __str__(self):
        return ('%s %s. %d square feet. Located at %s %s %s %d' %
               (self.__Modelname, self.__salestatus, self.__squarefeet, self.__address, self.__city, self.__state, self.__zipcode))
          
class Inventory:

    def __init__(self, starting_count=0, starting_inv={}):
        self.count = starting_count
        self.inv = starting_inv

    def __str__(self):
        return str(self.inv)

    def add_home(self, home):
        if type(home) == Home:
            self.count += 1
            self.inv[self.count] = vars(home)
            print(self.inv)
        else:
            print('Must be a valid home')
            new_home = input("Would you like to create a home and add it to the inventory? (Y/N): ")
            if new_home[0].lower() == 'y':
                new_home = Homes()
            else:
                print('Inventory has not been updated.')

    def del_home(self, inventory_id):
        if inventory_id in self.inv.keys():
            print("Are you ready to delete the entry?")
            user_input = input("Y/N: ")
            if user_input[0].lower() == 'y':
                del self.inv[inventory_id]
                print("Entry successfully deleted.")
            else:
                print("Inventory has not been modified.")
        else:
            print("Entry was not found in the inventory.")

    def update_home(self, inventory_id):
        if inventory_id in self.inv.keys():
            print("Are you ready to modify the entry?")
            user_input = input("Y/N: ")
            if user_input[0].lower() == 'y':
                try:
                    ref = self.inv[inventory_id].copy()
                    ref['Modelname'] = input(f"Model name: {ref['Modelname']} --> ")
                    ref['salestatus'] = input(f"Sale status (Sold, Available, Under Contract: {ref['salestatus']} --> ")
                    ref['squarefeet'] = int(input(f"Square feet: {ref['squarefeet']} --> "))
                    ref['address'] = input(f"Address: {ref['address']} --> ")
                    ref['city'] = input(f"City: {ref['address']} --> ")
                    ref['state'] = input(f"State: {ref['state']} --> ")
                    ref['zipcode'] = int(input(f"Zip code: {ref['zipcode']} --> "))
                    self.inv[inventory_id] = ref
                    print(f"Successfully updated.")
                except ValueError:
                    print("You entered invalid information, please try again.")
                    self.update_home(inventory_id)
            else:
                print("Inventory has not been modified.")
        else:
            print("Entry was not found in the inventory.")

    def view_inventory(self):
        print("FIXME: Develop view functionality")


def main():
    menu = {}
    menu[1] = "Add a home."
    menu[2] = "Remove a home."
    menu[3] = "Update a home."
    menu[4] = "View inventory."
    menu[5] = "Exit."

user = True
while user: 
    print("""
    1. Add a home.
    2. Remove a home.
    3. Update a home.
    4. View inventory.
    5. Exit.
    """)

    user_input = int(input("What would you like to do? "))

    if user_input == 1:
        Inventory().add_home()
    elif user_input == 2:
        Inventory().del_home()
    elif user_input == 3:
        Inventory().update_home()
    elif user_input == 4:
        Inventory().view_inventory()
    elif user_input == 5:
        print("Goodbye.")
    else:
        print("Enter a valid option.")


if __name__ == "__main__":
    main()

Thank you!!

I think the issue is that you need to initialize an instance of your Inventory class in main(). Classes are like blueprints, to use their goods you need to build it first. In your inventory method you build a new instance of Homes with new_home = Homes(), you just need to do the same for Inventory.
The exception to this is static class methods, but that’s a different topic, you really want a unique instance here.

Something like:

# Call class methods from this instance of Inventory
user_inventory = Inventory()

# example
user_input = int(input("Select An Option"))
if user_input == 1:
    user_inventory.add_home()
elif user_input == 2:
    user_inventory.del_home()
...
1 Like

Oh my gosh, you’re totally right. I had been staring at that code for so long it was starting to blur. Thank you so much for your help!