Budget App - Spend Chart Printing Unexpected Text

My Issue:

Balance ( Food ):  973.96 

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96

***********Clothing***********
Transfer from Food       50.00
                        -25.55
Total: 24.45

*************Auto*************
initial deposit        1000.00
                        -15.00
Total: 985.0

Percentage spent by category
100|          
 90|          
 80|          
 70|          
 60| o        
 50| o        
 40| o        
 30| o        
 20| o  o     
 10| o  o  o  
  0| o  o  o  
    ----------
     F  C  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g     
None
.Percentage spent by category
100|          
 90|          
 80|          
 70|    o     
 60|    o     
 50|    o     
 40|    o     
 30|    o     
 20|    o  o  
 10|    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  
F...F.F...
======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
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: None != 'Percentage spent by category\n100|      [384 chars] t  ' : Expected different chart representation. Check that all spacing is exact.

======================================================================
FAIL: test_to_string (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app/test_module.py", line 83, in test_to_string
    self.assertEqual(actual, expected, 'Expected different string representation of object.')
AssertionError: '****[77 chars]s, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33\n' != '****[77 chars]s, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33'
  *************Food*************
  deposit                 900.00
  milk, cereal, eggs, bac -45.67
  Transfer to Entertainme -20.00
- Total: 834.33
?              -
+ Total: 834.33 : Expected different string representation of object.

======================================================================
FAIL: test_transfer_no_funds (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app/test_module.py", line 75, in test_transfer_no_funds
    self.assertEqual(good_transfer, False, 'Expected `transfer` method to return `False`.')
AssertionError: True != False : Expected `transfer` method to return `False`.

----------------------------------------------------------------------
Ran 11 tests in 0.011s

FAILED (failures=3)

Your code so far

class Category:
  def __init__(self, name):
    self.name = name
    self.deposits = 0.0
    self.withdrawls = 0.0
    self.balance = 0.0
    self.ledger = []

  def get_balance(self):
    return self.balance

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

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

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

  def transfer(self, amount, destination):
    try:
      self.withdraw(amount, "Transfer to {}".format(destination.name))
      destination.deposit(amount, "Transfer from {}".format(self.name))
    except False: return False
    else: return True

  def __str__(self):
    string = self.name.center(30, '*') + '\n'
    for ledge in self.ledger:
      string += ledge["description"][:23].ljust(
        23) + "{:.2f}".format(ledge["amount"])[:7].rjust(7) + '\n'
    string += "Total: " + str(self.balance) + '\n'
    return string

def create_spend_chart(categories):
# Function Data
  total_expenses = sum(c.withdrawls for c in categories)
  m_length = max(len(c.name) for c in categories)
  col_wid = 12 + m_length
  columns = [[]]
# First column for graduation and extra space
  for i in range(col_wid):
    if i < 11: columns[0].append(str(10*(10-i)).rjust(3) + "| ")
    elif i == 11: columns[0].append("    -")
    else: columns[0].append("     ")
# Additional columns for displaying percentages
  for c in categories:
    new_col = []
    for i in range(11):
      if int(10 * (c.withdrawls / total_expenses)) >= 10 - i:
        new_col.append("o  ")
      else:
        new_col.append("   ")
    new_col.append("---")
    for i in range(m_length):
      if len(c.name) > i:
        new_col.append(c.name[i] + "  ")
      else:
        new_col.append("   ")
    columns.append(new_col)
# Print
  print("Percentage spent by category")
  for i in range(col_wid):
    line = ""
    for c in columns:
      line += c[i]
    print(line)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0

Challenge: Budget App

Link to Code:

Link to the challenge:

And

There is a very small difference here.

That solves error 1.

It would be much easier if you post the link to your code.

I’m not sure if this is valid syntax.

If nothing else, writing the else block on the same line is non-standard, but it also might not do what you want.

You’re printing here when the instructions say to return a string.

I’m not sure if this is valid syntax.

I got it from here.

If nothing else, writing the else block on the same line is non-standard, but it also might not do what you want.

Well if it doesn’t then I’ve been lucky up until now. I’ve done it on previous projects with no issue.

Note that the link you provided does not show an example of sticking everything on one line. Python doesn’t always do what you expect when you stick everything on one line. And even when it does, it is still not standard.

If you like it, you are free to use alternate syntax. Just understand that atypical syntax makes it harder for others to read your code and sometimes does not do the same thing as the standard syntax.


In any case, your transfer function does not raise an exception, so using try-except won’t work here.

1 Like

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