Not passing chart test - Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

My code is passing every test for the budget app project, except for the very last one that is about returning the chart, but when I look at them I have no clue what the problem is, and I’ve tried editing it around many times. Any assistance is appreciated!

Your code so far

class Category:
    categories = []
    def __init__(self, category):
        self.category = category
        self.ledger = []
        self.total_spent = 0
        Category.categories.append(self)

    def __str__(self):
        items = []
        title_line = self.category.center(30, '*')
        for item in self.ledger:
          description = item['description'][:23]
          amount = "{:.2f}".format(item['amount'])
          items.append(f"{description:23}{amount:>7}")
        ledger_str = ('\n').join(items)
        return f"{title_line}\n{ledger_str}\nTotal: {self.get_balance():.2f}"

    def deposit(self, amount, description=''):
        self.ledger.append({"amount": amount, "description":description})
    
    def withdraw(self, amount, description=''):
        if self.check_funds(amount) == True:
            self.ledger.append({"amount": amount * -1, "description": description})
            self.total_spent += amount  
            return True
        else:
            return False

    def get_balance(self):
        balance = 0
        for entry in self.ledger:
          balance += entry['amount']
        return balance
    
    def transfer(self, amount, category):
        if self.check_funds(amount):
          self.withdraw(amount, f"Transfer to {category.category}")
          category.deposit(amount, f"Transfer from {self.category}")
          return True
        else:
          return False
    def check_funds(self, amount):
        if amount > self.get_balance():
            return False
        else:
            return True
    

def create_spend_chart(categories):
    chart_title = 'Percentage spent by category'
    spend_categories = []
    result = ''
    chart_y = ''
    chart_x = ''
    for category in Category.categories:
      spend_categories.append(category.category)

    # calculate percentages
    total = sum(category.total_spent for category in categories)
    category_percentages = [(category.total_spent / total) * 100 for category in categories]

    # y-axis
    for i in range(100, -10, -10):
      chart_y += f'{i:>3}|'
      for percentage in category_percentages:
          if percentage >= i:
              chart_y += ' o '
          else:
              chart_y += '    '
      chart_y += '\n'

    # Horizontal line
    chart_y += "    " + "-" * (len(Category.categories) * 3 + 1) + "\n"

    category_spacing = 2
    cat_space = 5
    end_space = 6
    # vertical categories
    for i in range(len(max(spend_categories, key=len))):
      for j, category in enumerate(spend_categories):
          if i < len(category):
            if j == 0:
                result += ' ' * cat_space
            result += category[i]
          else:
            result += ' ' * end_space
          if j < len(spend_categories) - 1:
            result += ' ' * category_spacing
      result += '\n'

    chart_y += result
    # return chart_y
    chart_title += '\n' + chart_y
    return chart_title

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Budget App

Use F12 to look at the browser console output in the dev tools, it will show a more verbose error.

Educated guess, your chart ends and/or begins with a \n that isn’t expected.

Been messing around with both, but no luck so far :confused: Ty anyways

Did you see the error in the console? Can you paste it, or a screenshot, here?

The error is this:

AssertionError: 
'Perc[35 chars]       \n 90|            \n 80|            \n [350 chars]  \n' 
!= 
'Perc[35 chars]     \n 90|          \n 80|          \n 70|   [339 chars] t  '

Does this help? Your output is first, the expected output is second. You can see there is a spacing problem, and your output ends with a \n

okay, I’ll attempt to address that, thanks for finding that

1 Like

Do you see where to find it now? Will come in handy all the time

Yes I see where to find that error now in the console, handy indeed! However I am still struggling to figure out how to fix this error, the whitespace stuff is really frustrating…

It looks like you are adding two extra spaces at the end of each line.