Create_spend_chart space issue on budget APP for Python

I’m not sure how to tell the exact difference between my output and the test modules output. I would like to be able to see where its outputting spaces and newlines, because maybe then I could just fix it.

I’m only missing the last test, for the create_spend_chart, so…

Heres my code:

class Category:
  
  multi_ledger = []
  

  def __init__(self, name):
    self.name = name
    self.balance = 0
    self.ledger = []

    

  @classmethod
  def percent_spent(cls):

    percents = []
    
    total = 0

    for spot in Category.multi_ledger:
      
      total += spot['amount']

    count = 0
    for spot in Category.multi_ledger:
      percents.append((spot['amount'] / total) * 100)
      count += 1



    
    return Category.multi_ledger

  

  def __repr__(self):

    title = f"{self.name:*^30}\n"
    lines = ""
    total = f"Total: {self.balance}"
    for x in self.ledger:
      
      line = f"{x['description']:<23.23}" + f"{x['amount']:>7.2f}" + "\n"
      lines += line

    output = title + lines + total
    return output

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

  def withdraw(self, amount, description=""):
    if(self.check_funds(amount)) == True:
      
      self.ledger.append({"amount": -amount, "description": description})
      Category.multi_ledger.append({"name": self.name.lower() ,"amount": -amount, "description": description})
      self.balance = self.get_balance()
      return True
    else:
      return False

  def get_balance(self):

    bal = 0
    
    for items in self.ledger:
      bal += items['amount']
    
    return bal
     

  def transfer(self, amount, cat):
    if(self.check_funds(amount)) == True:

      self.withdraw(amount, f"Transfer to {cat.name}")
      cat.deposit(amount, f"Transfer from {self.name}")
      return True
    else:
      return False

  def check_funds(self, amount):
    if self.get_balance() >= amount:
      return True
    else:
      return False
food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")

food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)


print("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  ")

def create_spend_chart(categories):
  
  percents = []
  str_cats = []
  cats = []

  for cat in categories:
    str_cats.append(cat.name.lower())
    

  
    
  for ledger in Category.multi_ledger:
    for cat in str_cats:
      if cat in ledger['name']:
        cats.append(ledger)
        
  arrange = []
  count= 0
  for key, val in enumerate(str_cats):
    
    for key2, val2 in enumerate(cats):
      if val2['name'] in val:
        arrange.append(val2)

  cats = arrange

  total = 0
  for cat in cats:
    total += cat['amount']

  for cat in cats:
    percents.append((cat['amount'] / total) * 100)
    
  
  

  height = len(max(str_cats, key = len))
  
  width = 5 + (3 * len(str_cats))

  
  title = "Percentage spent by category\n"
  bars = ""

  
  count= 0
  
  countdown = 100
  while countdown >= 0:

    bars += f"{countdown: >3}| "
    
    for p in percents:
      if p > countdown:
        bars += "o  "
      else:
        bars += "   "
    
      
    
    countdown -= 10
    count += 1
    if countdown > -1: bars += "\n"

  

  dashes = "    -" + f"{'-'*3 * len(cats)}"

  

  downcats = ""

  
  count = 0
  while count <= height-1:
    downcats += "     "
    for cat in str_cats:
      try:
        if count == 0: 
          downcats += f"{cat[count]}  ".upper()
        else:
          downcats += f"{cat[count]}  "
      except:
        downcats += "   "
     
    if count < height -1: 
      downcats += "\n"
    count += 1
  
  

  output = f"{title}{bars}\n{dashes}\n{downcats}"
  
  
  return output

print(create_spend_chart([business, food, entertainment]))

Please help me, oh brialiant internet people. XD

I tried to format your code. A link to your repl would help because this code is confusing if it is all from the same file.

Here’s a link: https://replit.com/@JohnGrout/boilerplate-budget-app-2#budget.py

I don’t get it. I figured out how to compare the strings spaces and newlines, and it looks like they are exactly the same…??? weird… anyways.

This is the error I’m getting:

python main.py
.F…

FAIL: test_create_spend_chart (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app-2/test_module.py”, line 101, in test_create_spend_chart
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
AssertionError: 'Perc[35 chars] \n 90| \n 80| [447 chars] t ’ != 'Perc[35 chars] \n 90| \n 80| \n 70| [339 chars] t ’
Diff is 963 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.


Ran 11 tests in 0.010s

FAILED (failures=1)

I figured it out.

I was getting errors because of the categories created in the main.py file

I guess my code didnt like them for some reason, so I commented them out and POOF!

VICTORY!!! XD

Thanks for your attempt at helping me… XD

1 Like

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