Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I don’t know what the error is in my code, apparently it is fine. Thanks in advance for your help.

Your code so far

class Category:

def __init__(self, category=""):
    self.ledger = []
    self.budget = 0  
    self.category = category
    self.Category = []
    self.Category += category

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

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

def get_balance(self):
    return self.budget 
    
def transfer(self, amount, another_budget):
    if self.check_funds(amount):
        self.budget -= amount
        self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_budget.category}'}) #self.withdraw(amount, description = f"Transfer to {another_budget}")
        another_budget.deposit(amount, description = f'Transfer from {self.category}')
        return True
    else: 
        return False

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

# Varios códigos sobre el print que no son explicados hasta el momento en freeCodeCamp:    
def __str__(self):
    printar = ""
    titulo = self.category.center(30, "*")
    printar += titulo
    for item in self.ledger:
        description = item["description"]
        amount = item["amount"]
        printar += "\n"
        printar += f'{description[0:23]:23}{amount:7.2f}'
    printar += "\n"
    printar += "Total: "
    printar += format(self.budget, ".2f")
    return printar ## and presupuesto de cada categoria: str(self.budget)

“”“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(food)”“”

food = Category(“Food”)
clothing = Category(“Clothing”)
auto = Category(‘Auto’)

food.deposit(1000, “deposit”)
food.withdraw(600, “groceries”)
food.withdraw(165.60, “books”)
food.withdraw(175.10, “books”)
food.transfer(50, clothing)

clothing.deposit(1000, “deposit”)
clothing.withdraw(200, “groceries”)

auto.deposit(1000, “deposit”)
auto.withdraw(100, “groceries”)

#print(type(food.category))

def create_spend_chart(categories=):
str_output = f"Percentage spent by category\n"
divisores_diez = [“100”, " 90", " 80", " 70", " 60", " 50", " 40", " 30", " 20", " 10", " 0"]
Total = 0
percentage_cat =
barra =
barrafinal = #Lista de cadenas final de los circulos de la barra acorde a sus porcentajes
CATEGORIES = #Lista de categorias ingresadas en la función
CATEGORIESFINAL =
lineas = “-”
for cat in categories:
Total += cat.budget
percentage_cat.append(cat.budget)
CATEGORIES.append(cat.category)

percentage_cat1 = list(map(lambda x: int(100 * (x / Total)), percentage_cat)) #Porcentaje de cada categoria puesta en la función
cantidad_barra = list(map(lambda x: int(10 * (x / Total)) + 1, percentage_cat))

for j in cantidad_barra:
    barra.append(j * "o")

for k in barra:
    while len(k) < 11:
        k += " "
    barrafinal.append(k[::-1])

if len(categories) == 1:
    for a, b in zip(divisores_diez, list(barrafinal[0])):
        str_output += f'{a}| {b}\n'
elif len(categories) == 2:
    for a, b, c in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1])):
        str_output += f'{a}| {b}  {c}\n'    
elif len(categories) == 3:
    for a, b, c, d in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1]), list(barrafinal[2])):
        str_output += f'{a}| {b}  {c}  {d}\n'
elif len(categories) == 4:
    for a, b, c, d, f in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1]), list(barrafinal[2]), list(barrafinal[3])):
        str_output += f'{a}| {b}  {c}  {d}  {f}\n'            

str_output += f"    {len(categories)*'---'}-\n"
max_len = 0

if len(CATEGORIES) == 1:
    max_len = len(CATEGORIES[0])
if len(CATEGORIES) == 2:
    max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1])) 
if len(CATEGORIES) == 3:
    max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1]), len(CATEGORIES[2]))  
if len(CATEGORIES) == 4:
    max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1]), len(CATEGORIES[2]), len(CATEGORIES[3]))

for t in CATEGORIES:
    while len(t) <= max_len:
        t += " "
    CATEGORIESFINAL.append(t[::])    

if len(CATEGORIES) == 1:
    for x in CATEGORIESFINAL[0]:
        str_output += f'     {x}\n'
if len(CATEGORIESFINAL) == 2:
    for x, y in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1]):
        str_output += f'     {x}  {y}\n'   
if len(CATEGORIESFINAL) == 3:
    for x, y, z in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1], CATEGORIESFINAL[2]):
        str_output += f'     {x}  {y}  {z}\n'
if len(CATEGORIESFINAL) == 4:
    for x, y, z, q in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1], CATEGORIESFINAL[2], CATEGORIESFINAL[3]):
        str_output += f'     {x}  {y}  {z}  {q}\n'

eliminar = 4 + len(CATEGORIES)*3 + 1

str_outputt = print(str_output[:-eliminar])

return str_outputt

create_spend_chart([food, clothing, auto])


Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Which tests are you not passing?

You should check the dev tools console (Press F12) for a more detailed error.

Hello!

“create_spend_chart should print a different graphical representation. Check that all spacing is exact.” is the test that is not passing.

I honestly don’t understand how to use the dev tools console.

I have used a few test examples and apparently they all work.
Here I leave a code to facilitate the process of finding the error:

class Category:

    def __init__(self, category=""):
        self.ledger = []
        self.budget = 0  
        self.category = category
        self.Category = []
        self.Category += category
 
    def deposit(self, amount, description = ""):
        self.ledger.append({'amount': amount, 'description': description})
        self.budget += amount

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

    def get_balance(self):
        return self.budget 
        
    def transfer(self, amount, another_budget):
        if self.check_funds(amount):
            self.budget -= amount
            self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_budget.category}'})
            another_budget.deposit(amount, description = f'Transfer from {self.category}')
            return True
        else: 
            return False

    def check_funds(self, amount):
        return self.get_balance() >= amount
    
    # Varios códigos sobre el print que no son explicados hasta el momento en freeCodeCamp:    
    def __str__(self):
        printar = ""
        titulo = self.category.center(30, "*")
        printar += titulo
        for item in self.ledger:
            description = item["description"]
            amount = item["amount"]
            printar += "\n"
            printar += f'{description[0:23]:23}{amount:7.2f}'
        printar += "\n"
        printar += "Total: "
        printar += format(self.budget, ".2f")
        return printar 

food = Category("Food")
clothing = Category("Clothing")
auto = Category('Auto')

food.deposit(1000, "deposit")
food.withdraw(600, "groceries")
food.withdraw(165.60, "books")
food.withdraw(175.10, "books")
food.transfer(50, clothing)

clothing.deposit(1000, "deposit")
clothing.withdraw(200, "groceries")

auto.deposit(1000, "deposit")
auto.withdraw(100, "groceries")

def create_spend_chart(categories=[]):
    str_output = f"Percentage spent by category\n"
    divisores_diez = ["100", " 90", " 80", " 70", " 60", " 50", " 40", " 30", " 20", " 10", "  0"]
    Total = 0
    percentage_cat = []
    barra = []
    barrafinal = [] #Lista de cadenas final de los circulos de la barra acorde a sus porcentajes
    CATEGORIES = [] #Lista de categorias ingresadas en la función
    CATEGORIESFINAL = []
    lineas = "-"
    for cat in categories:     
        Total += cat.budget
        percentage_cat.append(cat.budget)
        CATEGORIES.append(cat.category)
        
    percentage_cat1 = list(map(lambda x: int(100 * (x / Total)), percentage_cat)) #Porcentaje de cada categoria puesta en la función
    cantidad_barra = list(map(lambda x: int(10 * (x / Total)) + 1, percentage_cat))

    for j in cantidad_barra:
        barra.append(j * "o")
    
    for k in barra:
        while len(k) < 11:
            k += " "
        barrafinal.append(k[::-1])
    
    if len(categories) == 1:
        for a, b in zip(divisores_diez, list(barrafinal[0])):
            str_output += f'{a}| {b}\n'
    elif len(categories) == 2:
        for a, b, c in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1])):
            str_output += f'{a}| {b}  {c}\n'    
    elif len(categories) == 3:
        for a, b, c, d in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1]), list(barrafinal[2])):
            str_output += f'{a}| {b}  {c}  {d}\n'
    elif len(categories) == 4:
        for a, b, c, d, f in zip(divisores_diez, list(barrafinal[0]), list(barrafinal[1]), list(barrafinal[2]), list(barrafinal[3])):
            str_output += f'{a}| {b}  {c}  {d}  {f}\n'            
    
    str_output += f"    {len(categories)*'---'}-\n"
    max_len = 0

    if len(CATEGORIES) == 1:
        max_len = len(CATEGORIES[0])
    if len(CATEGORIES) == 2:
        max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1])) 
    if len(CATEGORIES) == 3:
        max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1]), len(CATEGORIES[2]))  
    if len(CATEGORIES) == 4:
        max_len = max(len(CATEGORIES[0]), len(CATEGORIES[1]), len(CATEGORIES[2]), len(CATEGORIES[3]))

    for t in CATEGORIES:
        while len(t) <= max_len:
            t += " "
        CATEGORIESFINAL.append(t[::])    

    if len(CATEGORIES) == 1:
        for x in CATEGORIESFINAL[0]:
            str_output += f'     {x}\n'
    if len(CATEGORIESFINAL) == 2:
        for x, y in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1]):
            str_output += f'     {x}  {y}\n'   
    if len(CATEGORIESFINAL) == 3:
        for x, y, z in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1], CATEGORIESFINAL[2]):
            str_output += f'     {x}  {y}  {z}\n'
    if len(CATEGORIESFINAL) == 4:
        for x, y, z, q in zip(CATEGORIESFINAL[0], CATEGORIESFINAL[1], CATEGORIESFINAL[2], CATEGORIESFINAL[3]):
            str_output += f'     {x}  {y}  {z}  {q}\n'
    
    eliminar = 4 + len(CATEGORIES)*3 + 1

    str_outputt = print(str_output[:-eliminar])

    return str_outputt

create_spend_chart([food, clothing, auto])

You can find the relevant error here.

self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.') 

This means that the first line, actual, is yours and the second, expected, is what it should be.

AssertionError: None != 'Percentage spent by category\n100|      [384 chars] t  '

Your function output is “None”. Usually this means the function is print()ing the output instead of returning it, but that doesn’t look like the case here.

Any idea why it might be like this?

If I test it, it returns none:

print(create_spend_chart([food, clothing, clothing]))
>>> None
1 Like

An assertion error / diff 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!

1 Like

The explanation you provided was clear and helped me understand how to use the dev tools console.

Thank you very much for your help, it helped me to find the errors in my code.

Finally my code passed.

1 Like