I might be doing something wrong in the Budget App project, because the code is just fine

Tell us what’s happening:

The code seems to be doing what it’s supposed to do regarding outputs. However (as you’ll check with the code snippet below) there is a small nuance regarding a specific part. On the very first test case scenario, “Business” shows with 0% on expected, while my code has 10%. On my calculations, this is also right because Business equals 16% of the total. I don’t know where I did something wrong, I need some guidance here.

Your code so far

Here’s the whole thing:
Here’s the part that only interests us. I took some parts from test_module.py. It prints 2 outputs: mine, and expected.

class Category:
    def __init__(self, name):
        self.ledger = list()
        self.name = name
        self.balance = 0
        self.withdrawals = 0

    def __str__(self):
        output = ""

        output = output + "*************Food*************\n"
        for x in self.ledger:
            output = output + '%-23.23s' % (f'{x}')
            output = output

        return output

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

    def withdraw(self, amount=0, description=""):
        if (self.balance - amount) >= 0:
            self.ledger.append({
                "amount": (-abs(amount)),
                "description": description
            })
            self.balance = self.balance - amount
            self.withdrawals = self.withdrawals + amount

            return True

        else:
            return False

    def get_balance(self):
        return self.balance

    def transfer(self, amount, destinationcategory):
        if (self.balance - amount) >= 0:
            self.withdraw(amount, f"Transfer to {destinationcategory.name}")
            destinationcategory.deposit(amount, f"Transfer from {self.name}")
            return True
        else:
            return False

    def check_funds(self, amount):
        if amount > self.balance:
            return False

        else:
            return True


def create_spend_chart(categories):

    output = "Percentage spent by category\n"

    # \/ Percentages

    category_spent = list()
    total = 0
    for x in categories:
        total = total + x.withdrawals
        category_spent.append(x.withdrawals)

    base10 = lambda number: int(10 * round(float(number) / 10))  # rounds to base 10
    for x in range(len(category_spent)):  # % of total  of each category
        category_spent[x] = base10((category_spent[x] * 100) / total)

    # adds the o if the category has that percentage
    logic = lambda number: "o" if (number >= x) else " "
    for x in range(100, -10, -10):
        if x == 0:
            output = output + "  "  # 0
        elif x != 100:
            output = output + " "  # 90-10

        output = output + f"{x}|"  # All

        z = True

        for y in category_spent:
            if z:  # First number, single space instead of two
                z = False
                output = output + " "

            else:
                output = output + "  "

            output = output + f"{logic(y)}"

        output = output + "  \n"

    # \/ ----------
    output = output + "    "
    total = 4
    z = True
    for x in categories:
        if z:  # First category
            z = False
            output = output + "--"
            total = total + 2
            continue

        output = output + "---"
        total = total + 3

    output = output + "--\n"

    # \/ Category names

    for x in range(len(categories)):
        categories[x] = list(categories[x].name)

    for x in range(len(max(categories, key=len))):

        output = output + "     "

        for y in categories:
            y = list(y)

            try:
                output = output + y[x]
            except:
                output = output + " "

            output = output + "  "
        
        output = output + "\n"
    
    return output


food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")
        
# Test
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)

expected = "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  "
#self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')

#create_spend_chart([business, food, entertainment])
print("Mine: \n\n", create_spend_chart([business, food, entertainment]))
print("\nExpected: \n\n", expected)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Budget App

Link to the challenge:

Without diving into your code too much, the test run is showing that the rounding is not to spec. The README states that the bars in the graph should be rounded down, not just rounded. The sample food-clothing-auto graph should be 60-20-10 and your code is producing 70-20-10. This is likely to be affecting your unit tests as well.

Good luck.

That down is so sneaky!!! Thanks for the answer, that must be the problem. I’ll try and fix my code according to that.