Passing Category object into function results in error

I have an assignment where I create objects that need to communicate with each other. One of the requirements is to create a function named create_spend_chart which will display a chart with based on values within the object.

The problem is that when calling the function with a list of categories, I’m receiving an error. When I run this on my home computer, it works as expected and passing in multiple objects is crucial to not having to perform a lot of rework on my code. Is there any advice anyone can give on what is going on with with the freeCodeCamp environment is recognizing the list value as a list object rather than a Category object?

<class 'list'>
Traceback (most recent call last):
  File "", line 22, in <module>
    print(create_spend_chart([food, clothing, auto]))
  File "/home/runner/boilerplate-budget-app-5/", line 72, in create_spend_chart
    category = category_object.object_name()
AttributeError: 'list' object has no attribute 'object_name'
def create_spend_chart(*args):
    for category_object in args:
        category = category_object.object_name()
        # More code is used for processing.

# My Class definition
class Category:

    def __init__(self, category):
        self.category = category
        self.ledger = list()

    def __repr__(self):
        category_character_length = len(self.category)
        max_length = 30
        total = 0
        asterisksl = "*" * ((max_length-category_character_length)//2)
        asterisksr = "*" * (max_length-len(asterisksl)-category_character_length)

        ledger_contents = asterisksl + self.category + asterisksr

        for ledger_entry in self.ledger:
            total += ledger_entry['amount']
            amount = "{:.2f}".format(ledger_entry['amount'])
            ledger_contents += '\n' + ledger_entry['description'][:23].ljust(23) + amount.rjust(7)
        ledger_contents += '\nTotal: ' + "{:.2f}".format(total)
        ledger_contents += '\n'

        return ledger_contents

    def object_name(self):
        return self.category

    def deposit(self, amount, description=''):
        self.ledger.append({'amount': float(amount), 'description': description})
        return True

    def withdraw(self, amount, description=''):
        # balance = self.balance()
        if self.check_funds(amount):
            self.ledger.append({'amount': float(-amount), 'description': description})
            return True
            return False

    def get_balance(self):
        balance = 0
        if len(self.ledger) > 0:
            for ledger_entry in self.ledger:
                balance += ledger_entry['amount']
            return 0

        return balance

    def transfer(self, amount, other_category):
        if self.check_funds(amount):
            self.withdraw(float(amount), 'Transfer to ' + other_category.object_name())
            other_category.deposit(amount, 'Transfer from ' + self.category)
            return True
            return False

    def check_funds(self, amount):
        current_balance = self.get_balance()

        if float(amount) > current_balance:
            return False
            return True

food = Category('Food')
entertainment = Category('Entertainment')
gasoline = Category('Gasoline')
food.deposit(100, 'Starting balance')
entertainment.deposit(100, 'Starting balance')
food.withdraw(2, 'Date with this really super hot babe!')
entertainment.withdraw(26.78, 'Movies with the gang.')
entertainment.withdraw(32.16, 'Popcorn, soft drink, candy')
gasoline.deposit(126.78, 'Starting balance')
gasoline.withdraw(32.16, 'work week')
gasoline.withdraw(26.78, 'Weekend trip')
gasoline.withdraw(38.22, 'work week')
food.transfer(20, entertainment)

create_spend_chart(food, gasoline, entertainment)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Have you tried to print out the args in the create_spend_chart function? This would clear a bit why exactly the error is happening.

Bottom line is - one of these is not the expected way to call the function as it is currently defined:

create_spend_chart([food, gasoline, entertainment])
create_spend_chart(food, gasoline, entertainment)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.