Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

Describe your issue in detail here.
create_spend_chart should print a different chart representation. Check that all
spacing is exact.
The only x I got is this question. how to fix the problem? Please Help.

Your code so far

class Category:
    def __init__(self, category_name):
        self.category_name = category_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
        return False
    def get_balance(self):
        return sum(item['amount'] for item in self.ledger)

    def transfer(self, amount, destination_category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {destination_category.category_name}")
            destination_category.deposit(amount, f"Transfer from {self.category_name}")
            return True
        return False

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

    def __str__(self):
        länge = int((30 - len(self.category_name)) / 2)
        lines = [f"{'*' * länge}{self.category_name}{'*' * länge}"]
        total = 0
        for entry in self.ledger:
            description = entry['description'][:23]
            amount = '{:.2f}'.format(entry['amount'])
            total += entry['amount']
        lines.append(f'Total: {total:.2f}')  # Korrigiert die Ausgabe der Gesamtsumme
        return '\n'.join(lines)

def create_spend_chart(categories):
    category_name = []
    spent = []
    for category in categories:
        total_spent = sum(item['amount'] for item in category.ledger if item['amount'] > 0)

    total_spent = sum(spent)
    percentages = [(amount / total_spent) * 100 for amount in spent]

    chart = 'Percentage spent by category \n'
    for i in range(100, -1, -10):
        chart += f'{i:3}| '
        for percent in percentages:
            if percent >= i:
                chart += 'o  '
                chart += '   '
        chart += '\n'
    chart += '    ' + '-' * (len(categories) * 3 + 1) + '\n'

    max_name_length = max(len(name) for name in category_name)
    for i in range(max_name_length):
        chart += '     '
        for name in category_name:
            if i < len(name):
                chart += f'{name[i]}  '
                chart += '   '
        if i != max_name_length:
            chart += '\n'
    return chart

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/17.2 Safari/605.1.15

Challenge Information:

Scientific Computing with Python Projects - Budget App

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!

I suspect it’s this:

The percentage spent should be calculated only with withdrawals and not with deposits.

It looks like you are totalling total deposits and calculating using that.

I think I’m only taking the withdrawals with this part of the code: if item[‘amount’] > 0

What is amount, is it a withdrawl?

Use this example and print out the variables you are using:

food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
food.transfer(50, clothing)


Print your variables to troubleshot:

total_spent = sum(spent)
print("total spent", total_spent, spent)

Total withdrawls should be 76.04