Build a Budget App - create_spend_chart()

Tell us what’s happening:

i’ve been working on this certification going on 3 weeks now which is crazy. i can’t figure out why i keep getting caught up. it looks like its printing all of the categories added up as a float, i tried to get a sum() too, but it said its not iterable, i dont know how to separate it by the category. the math for percentages is simple enough to do, but i dont see where i can write the code yet and i can re-use the code from the rpg character maker to show the graph, but im stuck standing,

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []

    def deposit(self, amount, description=""):
        self.ledger.append({"amount": amount, "description": description})

    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append({"amount": -amount, "description": description})
            return True
        else:
            return False

    def get_balance(self):
        balance = 0
        for transaction in self.ledger:
            balance += transaction['amount']    
        return balance

    def check_funds(self, amount):
        return amount <= self.get_balance()

    def transfer(self, amount, destination_category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {destination_category.name}")
            destination_category.deposit(amount, f"Transfer from {self.name}")
            return True
        else:
            return False
    def __str__(self):
        
            method = '\n'.join(f"{transaction['description']:23.23}{transaction['amount']:>7.2f}" for transaction in self.ledger )
            return f"{self.name.center(30,'*')}\n{method}\nTotal: {self.get_balance()}"

def spreadsheet(*categories):
    for category in categories:
        print(category)
        print()

def create_spend_chart(*categories):
    print('Percentage spent by category\n')
    total_withdraw = 0
    for category in categories:
        for transaction in category.ledger:
            if transaction['amount'] < 0:
                total_withdraw += transaction['amount']
                print(total_withdraw)
                
    
    

food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.deposit(1000)
clothing.withdraw(100)
clothing.withdraw(30.14)
spreadsheet(food, clothing)
create_spend_chart(food, clothing)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App - Build a Budget App

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @josephg,

You will need to return a string that creates the chart, which is similar, but much more complex, to what you did in the RPG character challenge where you ended each line with a newline character.

Think of the pieces you will need to create this string:

  • a list of the categories
  • a list of withdrawals for each category
  • the sum of the withdrawals for all categories
  • the percent of category withdrawals to total withdrawals rounded down to the nearest 10
  • a list of the category names

Then think about the logic:

  • loop over a range of chart percentages to populate each line in the chart
  • loop over a range based on the longest category name to show each letter in each category name line by line

The rest is formatting.

Hope that helps…

Happy coding

I am so frustrated and lost with this code right now. It is just not clicking. im basically still stuck in the same spot. the list function doesnt work, i cant get a sum,.

I feel like im playing a guessing game right now, i am absolutely stumped.

ive fixed the problem with it loading the whole sequence, i used .floor to round it to the nearest int. i gave the function 4 arguments for the 4 categories, i know the sum is -300 but i cant get it to sum up. It keeps showing int isn’t iterable, but i cant figure that part out.

THIS IS MY UPDATED CODE:

def create_spend_chart(cat1, cat2 = None, cat3 = None, cat4 = None):

    categories = \[cat1,cat2,cat3,cat4\]

    for category in categories:

        if not category == None:

            total_withdraw = 0

            for transaction in category.ledger:

                if transaction\['amount'\] < 0:

                    total_withdraw += transaction\['amount'\]

            print(math.floor(total_withdraw))

                    

food = Category('Food')

auto = Category('Auto')

food.deposit(1000, 'initial deposit')

auto.deposit(1000, 'initial deposit')

food.withdraw(10.15, 'groceries')

food.withdraw(15.89, 'restaurant and more food for dessert')

clothing = Category('Clothing')

food.transfer(50, clothing)

food.withdraw(100.99)

clothing.withdraw(21.17)

auto.withdraw(200)

print(create_spend_chart(food, clothing, auto))

You should have a function outside the Category class named create_spend_chart(categories) that takes a list of categories and returns a bar-chart string.

Is your function taking a list?

remember that a list is a specific data type

I’m definitely a step closer now. thank you.

I have the percentages calculated for one category, but it wont calculate yet for the rest of them.