Build a Budget App - Build a Budget App

Tell us what’s happening:

Hi there. The only remaining test that is failing for me is number 16 - “Printing a category instance should give a different string representation of the object”. I have tried using the same exact data as is in the example, and my output looks identical to me, but the test is still failing. Is there anything obvious about my code that’s wrong, I’m very stuck.

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):
            withdrawal = 0-amount
            self.ledger.append({'amount': withdrawal, 'description': description})
            return True

        return False


    def get_balance(self):
        balance = sum([t.get("amount") for t in self.ledger])
        return balance


    def transfer(self, amount, receiver):
        if not self.check_funds(amount): return False
        else:
            self.withdraw(amount, "Transfer to " + receiver.name)
            receiver.ledger.append({'amount': amount, 'description': "Transfer from " + self.name})
            return True


    def check_funds(self, amount):
        balance = sum([t.get("amount") for t in self.ledger])
        return False if amount > balance else True


    def __str__(self):
        length = len(self.name)
        stars = int((30 - length) / 2)

        title_line = ("*" * stars) + self.name + ("*" * (30-length-stars))
        entries = ""

        for index, l in enumerate(self.ledger):
            if index == 0:
                i = "initial deposit"
                id = self.ledger[0]["amount"]
                entries += f"{i:<23}{id:>7.2f}\n"

            else:
                e = self.ledger[index]["description"]
                a = self.ledger[index]["amount"]
                entries += f"{e[0:22]:<23}{a:>7.2f}\n"


        total = float(sum([t.get("amount") for t in self.ledger]))

        return f"{title_line}\n{entries}Total: {total:.2f}"


#-------------------------------------------------
#CHART DRAWING

def create_spend_chart(categories):

    spentlist = []
    per = []

    # Find the total amount spent (excludes transfers to other accounts)

    for c in categories:
        spent = 0

        for t in c.ledger:
            if t["amount"] < 0 and "Transfer to" not in t["description"]: spent-= t["amount"]
            else: spent -= 0

        spentlist.append(abs(spent))

    totalspend = sum(spentlist)

    # Find the percentage spent per category

    for l in spentlist:
        cat_spent = (l/totalspend) * 100

        if round(cat_spent, -1) > cat_spent:
            per.append(round(cat_spent, -1) - 10)

        else:
            per.append(round(cat_spent, -1))


    # Figure out how to make the chart work now we have the data

    chart = "Percentage spent by category\n"

    for n in range (100, -10, -10):
        numbers = f"{n:>3}|"

        for p in per:
            if p >= n: onum = " o "
            else: onum = "   "

            numbers += onum
        numbers += " "

        chart += f"{numbers}\n"

    dashes = "-" * len(categories)*3
    chart += "    " + dashes + "-"

    names = [c.name for c in categories]
    maxlen = len(max(names, key = len))


    # Category names on the chart

    for i in range(0, maxlen):
        barname = "    "

        for n in names:
            if i < len(n): barname += f" {n[i]} "
            else: barname += "   "

        barname += " "
        chart += f"\n{barname}"

    return chart

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

AssertionError: '****[23 chars]***\ninitial deposit         900.00\nmilk, cer[64 chars]4.33'
             != '****[23 chars]***\ndeposit                 900.00\nmilk, cer[64 chars]4.33'

do you see the difference? your code output is the first, the second one is the expected

it looks like you hardcoded the title for the first operation, is there anywhere in the user stories to do this?

In addition to what ILM pointed out, it looks like you are not truncating the descriptions as expected.
Here’s the browser’s console output:

Looks like the “-” represents actual and “+” is expected.

I see, but that just makes me more confused. Is it not supposed to look exactly like the example in the user stories?

there isn’t written anywhere that you need to change the description of the first transaction tho

But in the example, it does change the name, so I thought that was required. Is that not the case? I mean, that’s clearly the problem in my case, it just looks like here it goes from “deposit” to “initial deposit” so I just thought that was what it had to be, if that was the example that was given.

For what it’s worth, I agree this example is misleading. Consider making a suggestion to change it at GitHub Issues.

Thank you for letting me know about that, I’ll put up a suggestion to that effect there. And sorry, I know this is the internet and its hard to read tone, but I was just confused by it is all, it works now so my immediate issue is solved. Thank you for the help!

ah, no need, it should be solved by next deployment

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