Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

Hi!! everyone … let plz help me to fix my wrong code. just remain 1 statement to meet the goal. Thx

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 item in self.ledger:
            balance += item['amount']
        return balance
        
    def check_funds(self, amount):
        return amount <= self.get_balance()
        
    def transfer(self, amount, category):
        if self.withdraw(amount, f"Transfer to {category.name}"):
            category.deposit(amount, f"Transfer from {self.name}")
            return True
        else:
            return False

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

    def get_spent(self):
        return sum(item['amount'] for item in self.ledger if item['amount'] < 0)

def create_spend_chart(categories):
    # Calcula el porcentaje de gasto para cada categoría
    total_spent = sum(category.get_spent() for category in categories)
    category_percentages = [(category.name, category.get_spent() / total_spent * 50) for category in categories]

    # Construye el encabezado del gráfico
    chart = "Percentage spent by category\n"

    # Construye las barras del gráfico
    for i in range(100, -1, -10):
        chart += f"{i:3}| "
        for _, percent in category_percentages:
            chart += "o  " if percent >= i - 10  else "   "
            # chart += "o  " if percent >= i  else "   "
        chart += "\n"

    # Construye la línea horizontal debajo de las barras
    chart += "    " + "-" * (len(categories) * 3 + 1) + "\n"

    # Encuentra la longitud máxima de los nombres de las categorías
    max_name_length = max(len(name) for name, _ in category_percentages)

    # Construye los nombres de las categorías debajo del gráfico
    for i in range(max_name_length):
        chart += "     "
        for name, _ in category_percentages:
            chart += name[i] + "  " if i < len(name) else "   "
        chart += "\n"

    return chart

# Ejemplo de uso
clothing_category = Category("Clothing")
auto_category = Category("Auto")
food_category = Category("Food")
food_category.deposit(100, "Initial deposit")
food_category.withdraw(20, "Groceries")
food_category.withdraw(30, "Restaurant")

categories = [food_category, clothing_category, auto_category]

print(create_spend_chart(categories))

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

2024-07-12_165954

check whether the percentage ranges are correctly calculated and aligned i think adjusting these elements should correct the spacing issues.

Yes! i know but cannot find the wrong code

You have category.get_spent() / total_spent * 50, but it should be category.get_spent() / total_spent * 100 to calculate the percentage out of 100.

image

when i changed to 50 then result as image below

You aren’t able to see the spacing or newlines entirely like this, especially at the end. You’ll need to use the more detailed diff error in the browser console area (press F12).

Here is a primer on how to use this:

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!