Build a Budget App Project - Build a Budget App Project Tests 21, 23, 24

Tell us what’s happening:

I compared and checked the number of spaces for the bar chart, the dashes, and the vertical titles and they all seem to be matching. Help? Not passing tests 21, 23, and 24 now. And I am also not really understanding what 24 is asking for.

Lastly, when I press F12 on my laptop, the wordings in the console is the same as that of the tests. I saw some people being able to bring up a different screen. Any tips? Thanks!

Edited to include tests:

[create_spend_chart] should correctly show horizontal line below the bars. Using three -
characters for each category, and in total going two characters past the final bar.

[create_spend_chart] chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces after the final category.

[create_spend_chart] should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.

Your code so far

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

    def __str__(self):
        # Find char_len of category
        char_len = 0
        for char in self.category:
            char_len += 1
        # Find length of *
        line_len = 30
        left_len = 0
        right_len = 0
        if (line_len - char_len) % 2 != 0:
            left_len = (line_len - char_len) // 2
            right_len = (line_len - char_len) // 2 + 1
        else:
            left_len = (line_len - char_len) // 2
            right_len = (line_len - char_len) // 2

        # Format main display body
        line_body = ""
        line_body_total = ""
        for line in self.ledger:
            description = line["description"][:23]
            amount = f"{line['amount']:7.2f}"
            desc_len = 0
            for char in description:
                desc_len += 1
            amount_len = 0
            for char in amount:
                amount_len += 1
            spaces = line_len - desc_len - amount_len
            line_body = description + (" " * spaces) + amount
            line_body = f"{line_body}\n"
            line_body_total += line_body

        # Format total display
        total = self.get_balance()
        total_display = f"Total: {total:.2f}"

        display = ""
        line_1 = ""
        for i in range(left_len):
            line_1 += "*"

        line_1 = ("*" * left_len) + self.category + ("*" * right_len)

        display = f"{line_1}\n{line_body_total}{total_display}"
        return display

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

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

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

    def transfer(self, amount, to_category):
        if self.check_funds(amount) == False:
            return False
        else:
            amount = -amount
            self.ledger.append({"amount": amount, "description": f"Transfer to {to_category.category}"})
            to_category.ledger.append({"amount": abs(amount), "description": f"Transfer from {self.category}"})
            return True

    def check_funds(self, amount):
        balance = self.get_balance()
        if amount > balance:
            return False
        else:
            return True

    # Calculate expenses total (balance without deposit)
    def expenses_total(self):
        total = 0
        for i in self.ledger:
            if i["amount"] < 0:
                total += abs(i["amount"])
        return float(f"{total:.2f}")

def create_spend_chart(categories):
    pass
    # Calculate percentage in categories
    category_expenses = [category.expenses_total() for category in categories]
    category_percentages = [round((category/sum(category_expenses))*100) for category in category_expenses]

    # Create chart
    chart = ""
        # Title
    chart_title = "Percentage spent by category"

        # left side labels
    labels = [f"{num}|" if num == 100 else f" {num}|" if num < 100 and num > 0 else f"  {num}|" for num in range(100, -1, -10)]

        # Bar chart
    line = ""
    for label in labels:
        line += f"{label} "
        for category_percentage in category_percentages:
            label = label.lstrip().replace("|", "")
            if category_percentage >= int(label):
                line += "o  "
            else:
                line += "   "
        line += "\n"

        # bottom line
    bottom_line = "    _" + "___" * len(categories) + "\n"
        # Category titles
    title_line = ""
    category_titles = [category.category for category in categories]
    max_len = max([len(category) for category in category_titles])

    for i in range(0, max_len):
        title_line += "     "
        for title in category_titles:
            try:
                title_line += f"{title[i]}  "
            except IndexError:
                title_line += "   "
        if i < max_len - 1:
            title_line += "\n"
        else:
            pass

    chart = f"{chart_title}\n{line }{bottom_line}\n{title_line}"
    return chart


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")
clothing.deposit(300, "deposit")
food.transfer(50, clothing)
auto = Category("Auto")
auto.deposit(500, "deposit")
auto.withdraw(30.32, "gas")
auto.withdraw(320.10, "tires")
# print(food)
# print(auto)
# print(auto.expenses_total())
categories = [food, clothing, auto]
print(create_spend_chart(categories))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Please say what those tests are and how your stuck working on them. Thanks

Just edited to include them above. Thank you.

You might have to scroll up but it should look like this

AssertionError: 'Perc[201 chars]n    __________\n\n     B  F  E  \n     u  o  [175 chars] t  ' != 'Perc[201 chars]n    ----------\n     B  F  E  \n     u  o  n [173 chars] t  '

The assertion error and 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

This is called a diff, and it shows you the differences between two files or blocks of code:

- 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

Here is your assertion error to compare:

AssertionError: 
'Perc[201 chars]n    __________\n\n     B  F  E  \n     u  o  [175 chars] t  ' != 
'Perc[201 chars]n    ----------\n     B  F  E  \n     u  o  n [173 chars] t  '
1 Like

Thank you, I’ll look into this. Is there a way to view these errors on my side? F12 doesn’t really do anything for me.

google how to open the browser console for the browser you are using, F12 is a common method, but if it doesn’t work, you will need to find the others.

1 Like

Thank you for explaining how to read the error code. Once I was able to find out how to open the console, I figured out the spacing issue. Also, I misused underscores instead of dashes…

1 Like