Build a Budget App - Build a Budget App

Tell us what’s happening:

That create spend chart is really tripping me up
I just don’t understand how to do this. Its super hard for me without visualisation Can anybody help me please?
Thanks a lot
Good Day
Mikhael

Your code so far

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

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

    def check_funds(self, amount): 
        funds = self.get_balance()
        if (amount > funds):
            return False
        else:
            return True

    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 transfer(self, amount, instance):
        withdraw_desc = f"Transfer to {instance.name}"
        deposit_desc = f"Transfer from {self.name}"
        if self.withdraw(amount, withdraw_desc):
            instance.deposit(amount, deposit_desc)
            return True
        else: 
            return False

    def __str__(self):
        title = self.name.center(30, "*")
        for entry in self.ledger:
            float_amount = f"{entry['amount']:.2f}"
            title += f"\n{entry['description'][:23].ljust(23)}"
            title += f"{float_amount[:7].rjust(7)}"
        title += f"\nTotal: {self.get_balance()}"
        return title

def create_spend_chart(categories):
    output = "Percentage spent by category\n"
    def sum_withdraw(category):
        total_withdraw = 0
        for entry in category.ledger:
            if entry["amount"] < 0:
                total_withdraw += entry["amount"]
        return total_withdraw
    def percentage(category):
        
    return output


    

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15

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 @MChance,

You have an error message showing in the console.

What are you doing to test your code? Where are you calling the function to see what it’s returning?

Happy coding

Hi dhess,

I’ve added the print call and it behaves as I expected : it shows only the header. I think my 2 main issues here are 1) imagining a bar-chart completely abstractly, without any actual data and 2) how to actually build said chart. It’s probably going to consist of concatenating lines of text to the output before returning it, but how are those lines supposed to be constructed? I’m actually completely lost here.

You can create data to test with like the example usage in User Story 4.

Yes, you will need to return a string and use concatenation. Just take it one line at a time. Looking at the example chart in User Story #5, what is the next thing you need to add to your title string? Before you tackle that, you might want to calculate the percentage spent for each category passed to your function.

Happy coding

I finally got it!! Thank you so much @dhess

I always have a hard time breaking down big problems like this. I really need to work on that

Happy coding to you!