Build a Budget App - Build a Budget App

Tell us what’s happening:

My chart looks like the example, but I’m still failing 6 tests. I’m not sure what I need to do to fix it, though.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = 0
        self.total_balance = 0
        self.withdrawals = 0

    #deposits amount into self and has optional description
    def deposit(self, amount, description=''):
        self.ledger.append({'amount': amount, 'description': description})
        self.balance += amount
        self.total_balance += amount
        

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


    def get_balance(self):
        return self.balance

    def check_funds(self, amount):
        if self.balance < amount:
            return False
        else:
            return True

    #transfer amount from self to dest
    def transfer(self, amount, dest):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {dest.name}')
            dest.deposit(amount, f'Transfer from {self.name}')
            return True
        else:
            return False
        
    
   # defines how an object will be printed as a string
    def __str__(self):
        entries = ''
        title = f"{self.name:*^30}"
        for entry in self.ledger:
            description = entry['description'][:23]
            amount = f"{entry['amount']:>7.2f}"
            entries += f"{description:<23}{amount}"
            total = f"Total: {self.balance:.2f}"

        return title+entries+total

        
        

def create_spend_chart(categories):
    #calculates total spending for all categories combined
    total_spent = sum(category.withdrawals for category in categories)
   #calculate percent spending for each category relative to total
    category_percentages = []

    for category in categories:
        if total_spent == 0:
           percent = 0
        else:
            percent = (category.withdrawals/total_spent)*100
            #round down to nearest 10
            
            category_percentages.append(percent)
    
    #display chart
    chart = "Percentage spent by category\n"
    for i in range(100, -1, -10):
        chart += f'{i:3}| '
        for percent in category_percentages:
            if round(percent, 1) >= i:
                chart += 'o  '
            else:
                ' '
        chart += '\n'
                
    #chart += '\n'
    line = f'----------'
    width = 14
    chart_line = f"{line:>{width}}"
    #print(f"{line:>5}")
    chart += chart_line
    chart += '\n'
    name = category.name
    max_name_length = max(len(category.name) for category in categories)
    for i in range(max_name_length):
        chart += "     " # 5 spaces for alignment
        for category in categories:
            if i < len(category.name):
                chart += f"{category.name[i]}  "
            else:
                chart += "   " # 3 spaces for alignment
        chart += "\n"
    chart = chart.rstrip()
    # Remove the last newline
    return chart
    
    

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)
clothing.withdraw(25, 't-shirt')
auto = Category('Auto')
auto.deposit(50, 'deposit')
auto.withdraw(20, 'oil change')
print(food)
print(clothing)
print(auto)
print(create_spend_chart([food, clothing, auto]))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build a Budget App - Build a Budget App

Hi @chloelyd

Looks like you may need line break here.

Happy coding

def __str__(self):
        entries = ''
        title = f"{self.name:*^30}\n"
        for entry in self.ledger:
            description = f"{entry['description'][:23]}"
            amount = f"{entry['amount']:>7.2f}"
            entries += f"{description:<23}{amount}\n"
            total = f"Total: {self.balance:.2f}"
        display = f"{title}{entries}{total}"
        return display

Okay, I fixed that. Do you know what else I need to fix?

What feedback are you getting from the tests?

Just start with the first test that’s failing.

Test 19: The height of each bar on the create_spend_chart should be rounded down to the nearest 10.

Seems like you are rounding ok.

However, open the browser console (F12) to see more detailed errors

AssertionError: 
'Perc[26 chars]100| \n 90| \n 80| \n 70| o  \n 60| o  \n 50| [283 chars]   t' !=
'Perc[26 chars]100|          \n 90|          \n 80|          [348 chars] t  '

Your output is first, the expected is second.

  1. Each line in create_spend_chart chart should have the same length. Bars for different categories should be separated by two spaces, with additional two spaces after the final bar.

You need to fill out each line with spaces so that the chart is squared off. If you highlight the example you can see the spacing better.