Build a Budget App - Build a Budget App

Tell us what’s happening:

I have finished the project and when testing manually everything looks perfect, but when I actually run the tests I am still stuck with these errors.

// running tests
16. Printing a Category instance should give a different string representation of the object.
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12

Your code so far

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

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

    def withdraw(self, amount, description=""):
        object = {
            "amount" : -amount,
            "description" : description
        }

        if self.check_funds(amount):
            self.ledger.append(object)
            return True
        else:
            return False

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

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

    def __str__(self):
        ast_count = 30 - len(self.name)

        print_items = []
        header = ""

        for _ in range(0, ast_count // 2):
            header += "*"

        header += self.name

        for _ in range(ast_count // 2, ast_count):
            header += "*"

        if len(header) != 30:
            header += "*"

        print_items.append(header)

        for item in self.ledger:
            new_line = ""
            desc = item["description"]
            cut_desc = desc[0:23]
            format_num = str(round(item["amount"], 2))
            spaces_needed = 30 - len(cut_desc) - len(format_num)
            new_line += cut_desc
            if spaces_needed:
                for _ in range(spaces_needed):
                    new_line += " "
            new_line += format_num
            print_items.append(new_line)

        print_items.append(f"Total: {self.get_balance()}")

        return "\n".join(print_items)
               




    def check_funds(self, amount):
        current_balance = self.get_balance()

        if amount > current_balance:
            return False
        else:
            return True


def create_spend_chart(categories):
    percentages = []
    cat_count = len(categories)
    total_spent = 0

    for cat in categories:
        for i in cat.ledger:
            if i["amount"] > 0:
                total_spent += i["amount"]

    print(total_spent)

    for cat in categories:
        spent = 0
        for i in cat.ledger:
            if i["amount"] > 0:
                spent += i["amount"]
        percentage = spent / total_spent * 100
        rounded = (percentage // 10) * 10
        percentages.append({"name" : cat.name, "percentage" : rounded})

    print(percentages)

    lines = ["Percentage spent by category",]

    for i in range(100, -10, -10):
        if i == 100:
            line = f"{i}| "
        elif i == 0:
            line = f"  {i}| "
        else:
            line = f" {i}| "

        for j in percentages:
            if i > j["percentage"]:
                line += "   "
            else:
                line += "o  "
        lines.append(line)

    bar_line = "    -"
    for _ in percentages:
        bar_line += "---"
    lines.append(bar_line)

    longest_name = 0

    for j in percentages:
        if len(j["name"]) > longest_name:
            longest_name = len(j["name"])
    
    for i in range(longest_name):
        new_line = "     "
        for j in percentages:
            try:
                new_line += j["name"][i] + "  "
            except Exception:
                new_line += "   "
        lines.append(new_line)
    
    return "\n".join(lines)

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 @Jon1122

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Did you try checking the browser console?

Did you try testing your output by calling the function?

  1. Printing a Category instance should give a different string representation of the object.

And here is an example of the output:

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96

Your output:

*************Food*************
deposit                   1000
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing       -50
Total: 923.96

Try testing the sample data in User Story 4

Thank you, of course it was a simple typo haha

Thank you, I did test with the sample data, but didn’t catch the lack of decimals on the numbers for some reason.