Creating Spend Chart

Tell us what’s happening:
I’m trying to do the Scientific Computing with Python Boilerplate Budget App Challenge. Everything is correct except the create_spend_chart function. I see I’m not the only one with this issue, but nothing looks similar to my error. The chart appears to be displaying in two places. One has the correct format and categories but is rounded down to the nearest 10’s place (out of 100) for some reason. The second displays something entirely incorrect.

Describe your issue in detail here.
Total: 24.45
Percentage spent by category
100|
90|
80|
70|
60|
50| o
40| o
30| o
20| o
10| o o
0| o o o
----------
F C A
o l u
o o t
d t o
h
i
n
g
.F…

Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app/test_module.py”, line 94, in test_create_spend_chart
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
AssertionError: 'Perc[77 chars]| \n 60| o \n 50| o \n [297 chars] t ’ != 'Perc[77 chars]| o \n 60| o \n 50| o \n [297 chars] t ’
Percentage spent by category
100|
90|
80|

  • 70|
    ? ^
  • 70| o
    ? ^
    60| o
    50| o
    40| o
    30| o
  • 20| o
    ? ^
  • 20| o o
    ? ^
    10| o o
  • 0| o o
    ? —
  • 0| o o o
    ? +++

    B F E
    u o n
    s o t
    i d e
    n r
    e t
    s a
    s i
    n
    m
    e
    n
    t : Expected different chart representation. Check that all spacing is exact.

Your code so far

def truncate(n):
  multiplier = 10
  return int(n * multiplier) / multiplier

def gettotal(categories):
  total = 0
  types = []
  for category in categories:
    total += category.get_withdrawls()
    types.append(category.get_withdrawls())
  roundtotal = list(map(lambda x: truncate(x/total), types))
  return roundtotal

def create_spend_chart(categories):
  percent = "Percentage spent by category\n"
  i = 100
  totals = gettotal(categories)
  while i >= 0:
    cspace = " "
    for total in totals:
      if total * 100 > i:
        cspace += "o  "
      else:
        cspace += "   "
    percent += str(i).rjust(3) + "|" + cspace + ("\n")
    i-= 10

  dspace = "-" + "---"*len(categories)
  names = []
  x_name = ""
  for category in categories:
    names.append(category.name)
  
  maxname = max(names, key=len)

  for x in range(len(maxname)):
    nameStr = '     '
    for name in names:
      if x >= len(name):
        nameStr += "   "
      else:
        nameStr += name[x] + "  "
    
    if(x != len(maxname) -1 ):
      nameStr += '\n'
    
    x_name += nameStr
  
  percent += dspace.rjust(len(dspace)+4) + '\n' + x_name
  return percent

class Category:

  def __init__(self, name):
    self.name = name
    self.ledger = list()

  def __str__(self):
    budget_section = f"{self.name:*^30}\n"
    items = ""
    total = 0
    for item in self.ledger:
      items += f"{item['description'][0:23]:23}" + f"{item['amount']:>7.2f}" + '\n'
      total += item['amount']

    result = budget_section + items + "Total: " + str(total)
    return result

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

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

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

  def transfer(self, amount, category):
    if(self.check_funds(amount)):
      self.withdraw(amount, "Transfer to " + category.name)
      category.deposit(amount, "Transfer from " + self.name)
      return True

    return False

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

  def get_withdrawls(self):
    total = 0
    for item in self.ledger:
      if item["amount"] < 0:
        total += item["amount"]
    return total
'''
'''
**Your browser information:**

Google Chrome

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36</code>

**Challenge:**  Budget App

**Link to the challenge:**
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/budget-app

The first chart is a demo chart and the second one is the differences from what you produced (- lines) compared to what is expected (+ lines). This shows the problem:

-  70|          
?         ^
+  70|    o     
?         ^

-  20|    o     
?            ^
+  20|    o  o  
?            ^

-   0|    o  o  
?      ---
+   0| o  o  o  
?            +++

You are missing a o in each column, which means you are rounding down twice. One is here:

and the other is here and appears to be unintentional:

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