Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

My code seems to be fine, the output is the exact same but it fails the last test. I found a post in this forum suggesting locating assertion errors but I couldn’t figure out how to test it on my own.

Your code so far

from decimal import *
getcontext().prec = 4

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

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

    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})
            self.expenses.append(-amount)
            return True
        return False

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

    def transfer(self, amount, category):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {category.name}')
            category.deposit(amount, f'Transfer from {self.name}')
            return True
        return False

    def check_funds(self, amount):
        if self.get_balance() >= amount:
            return True
        return False
        
    def spent(self):
        spent = 0
        for value in self.expenses:
            spent += Decimal(value)
        return spent

def create_spend_chart(categories):
    title = 'Percentage spent by category'
    line = '\n'
    total_expenses = 0
    percentage = {}
    per_str = {}
    rounder_per = {}
    dashes = '    '
    names = []

    for category in categories:
        dashes += '---'
        names.append(category.name)
        total_expenses += category.spent()
    percentage = {category.name: float((category.spent()/total_expenses)*100)  for category in categories}
    rounded_per = {category.name: int(str(percentage[f'{category.name}'])[0] + '0') for category in categories}
    
    dashes += '-'
    axis = ''
    maior = max(names, key=len)
    for i in range(len(maior)):
        vertical_name = '     '
        for name in names:
            if i>=len(name):
                vertical_name += '   '
            else:
                vertical_name += name[i] + '  ' 
        
        if i!=(len(maior) - 1):
            vertical_name += '\n'
        axis += vertical_name


    for num in range(100, -10, -10):
        line += f'{str(num).rjust(3)}|'
        for category in categories:
            if rounded_per[f'{category.name}']>=num:
                line += ' o '
            else:
                line += '   '
        line += '\n'
    
    output = title + line + dashes + '\n' + axis           
    return output

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Budget App

what have you tried for debugging?

I saw another post here (Scientific Computing with Python Projects - Budget App - #3 by pkdvalis) and tried to follow it, but couldn’t figure how to use AssertionError. Tried searching for it and didn’t understand either. I tried adding “AssertionError: my input != desired input” to the code and nothing happened.

I also tried to run the code in VS and Colab and it was fine in both of them

Unfortunately this project has moved to a new platform, which does not give assertion errors, so this will not work.

This new platform gives very little information about the last error, but you can use the information in this thread:

https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-budget-app/677107/5

This is the expected output:

Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t

Print your chart like this to see the raw string output to compare:

print(repr(chart))

Ok I’m a little confused. The output you are giving me have different categories (Business, Food, Entertainment) from the one available in the example (Food, Clothing, Auto).

This is the info that the test uses.

Actually I guess you will need all of this info to test properly:

food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")    
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")

food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)

print(repr(create_spend_chart([business, food, entertainment]))

You can add this to the end of your code to test the function against the expected output I gave above. Then you will be able to see what’s wrong

1 Like

Thank you!! I finally figured it out. My code had a logical error: I was adding a 0 to the end of the percentage even when it was lower than 10%. In this test you sent me, business was outputing 70% for that reason. In the example test none of the percentages were lower than 10% so my code was running well and that error got past me.

Adding a simple conditional to the percentages solved it! :smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.