Spacing problem in Budget App - Scientific computing with python

Help please!

I’ve completed the project, but i’m having issues with create_spent_chart() function. For some way there’s a spacing error that i can’t find out. I’m just stuck on this since one week, please help me. Thanks to you all.

Link to the repl: https://replit.com/@EnricoNicosia1/boilerplate-budget-app-1#budget.py

def create_spend_chart(categories):
    #Python will unpack the arguments as a single tuple with all the arguments.
    x = "Percentage spent by category\n"
    withdrawals = list()
    percentages = list()
    wdrTotal = 0
    names = list()

    for category in categories:  #single withdrawals for each class
        withdrawals.append(category.wdr)
        wdrTotal += category.wdr
        cnam = category.name
        names.append(cnam)

    for w in withdrawals:  #rounded percentages
        percentages.append(round(((w * 100) / wdrTotal) / 10) * 10)

    counter = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
    is100 = True

    for i in counter:  #spacing needed for the first line
        if (is100):
            x += str(i) + "| "
            is100 = False
        else:
            x += " " + str(i) + "| "

        for perc in percentages:  #adds parcentages to the graphic
            if (perc >= i):
                x += "o  "
                perc -= 10
            else:
                x += "   "

        x += "\n"

    x += "  0| o  o  o  \n    " + "---" * len(
        categories) + "-\n"  #0 line is always the same

    maxLen = 0
    nameLetters = list() #list of lists with each letter of each name

    for i in names:  #defines max length for names' columns
        if (len(i) >= maxLen):
            maxLen = len(i)

        nameLetters.append(list(i))

    for item in nameLetters: #adds withespaces to names that are shorter than the max
        if len(item) < maxLen:
            while len(item) < maxLen:
                item.append(" ")

   

    for n in range(maxLen): #sums up all to the x string
        x += "     "
        for z in range(len(nameLetters)):  
            x += nameLetters[z][n] + "  "

        x += "\n"

    print(maxLen)

    return x


class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = list()
        self.wdr = 0 #total of the withdrawals for each category

        #Is a sort of constructor that gives an "output value" to the instances
    def __str__(self):
        cart = ""
        total = 0

        first = f"{self.name:*^30}\n"

        for i in self.ledger:  #print method for displaying
            cart += f"{i['description'][0:23]:23}" + f"{i['amount']:>7.2f}" + '\n'

            total += i['amount']

        return (first + cart + "Total: " + str(total))

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

    def withdraw(self, amount: float, description=""):

        check = self.check_funds(amount)

        if check:
            self.ledger.append({"amount": -amount, "description": description})
            self.wdr += amount  #refeers to line 72
            return True
        else:
            return False

    def check_funds(self, amount):

        if (self.get_balance() >= amount):
            return True
        else:
            return False

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

        return balance

    def transfer(self, wdraw, category):
        check = self.check_funds(wdraw)
        if check:
            self.withdraw(wdraw, "Transfer to " + category.name)
            category.deposit(wdraw, "Transfer from " + self.name)
            return True
        else:
            return False

Error:

F.........
======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app-1/test_module.py", line 143, in test_create_spend_chart
    self.assertEqual(
AssertionError: 'Perc[170 chars] 10| o  o  o  \n  0| o  o  o  \n    ----------[206 chars]  \n' != 'Perc[170 chars] 10|    o  o  \n  0| o  o  o  \n    ----------[204 chars] t  '
  Percentage spent by category
  100|          
   90|          
   80|          
   70|    o     
   60|    o     
   50|    o     
   40|    o     
   30|    o     
   20|    o  o  
-  10| o  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  
?               -
+            t   : Expected different chart representation. Check that all spacing is exact.

----------------------------------------------------------------------
Ran 11 tests in 0.008s

FAILED (failures=1)

Your browser information:

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

Challenge: Budget App

Link to the challenge:

Please provide a link to your replit or at least the actual error message.

2 Likes

Here it is, sorry but i’m new to coding and forum’s world, thank you

That’s the part that’s wrong - first is your output, second is the expected output (look into the test_module.py for reference).
Reason for the error is that you are using the “round” function, which follows basic rounding rules BUT the task was to always round down.

1 Like

I’ve found the problem, ty! There’s only one last bug in the code that seems invisible, what could it be?

def create_spend_chart(categories):
    #Python will unpack the arguments as a single tuple with all the arguments.
    x = "Percentage spent by category\n"
    withdrawals = list()
    percentages = list()
    wdrTotal = 0
    names = list()

    for category in categories:  #single withdrawals for each class
        withdrawals.append(category.wdr)
        wdrTotal += category.wdr
        cnam = category.name
        names.append(cnam)

    for w in withdrawals:  #rounded percentages
        perc= int((w/wdrTotal)*100)
        value= round(perc/10)*10   
        if(value>perc):
          value-= 10        
        percentages.append(value)

    counter = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
    is100 = True

    for i in counter:  #spacing needed for the first line
        if (is100):
            x += str(i) + "| "
            is100 = False
        else:
            x += " " + str(i) + "| "

        for perc in percentages:  #adds parcentages to the graphic
            if (perc >= i):
                x += "o  "
                perc -= 10
            else:
                x += "   "

        x += "\n"

    x += "  0| o  o  o  \n    " + "---" * len(
        categories) + "-\n"  #0 line is always the same

    maxLen = 0
    nameLetters = list() #list of lists with each letter of each name

    for i in names:  #defines max length for names' columns
        if (len(i) >= maxLen):
            maxLen = len(i)

        nameLetters.append(list(i))

    for item in nameLetters: #adds withespaces to names that are shorter than the max
        if len(item) < maxLen:
            while len(item) < maxLen:
                item.append(" ")

   

    for n in range(maxLen): #sums up all to the x string
        x += "     "
        for z in range(len(nameLetters)):  
            x += nameLetters[z][n] + "  "

        x += "\n"

    return x


class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = list()
        self.wdr = 0 #total of the withdrawals for each category

        #Is a sort of constructor that gives an "output value" to the instances
    def __str__(self):
        cart = ""
        total = 0

        first = f"{self.name:*^30}\n"

        for i in self.ledger:  #print method for displaying
            cart += f"{i['description'][0:23]:23}" + f"{i['amount']:>7.2f}" + '\n'

            total += i['amount']

        return (first + cart + "Total: " + str(total))

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

    def withdraw(self, amount: float, description=""):

        check = self.check_funds(amount)

        if check:
            self.ledger.append({"amount": -amount, "description": description})
            self.wdr += amount  #refeers to line 72
            return True
        else:
            return False

    def check_funds(self, amount):

        if (self.get_balance() >= amount):
            return True
        else:
            return False

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

        return balance

    def transfer(self, wdraw, category):
        check = self.check_funds(wdraw)
        if check:
            self.withdraw(wdraw, "Transfer to " + category.name)
            category.deposit(wdraw, "Transfer from " + self.name)
            return True
        else:
            return False

Here’s the error:

======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app-4/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[364 chars]         m  \n           e  \n           n  \n           t  \n' != 'Perc[364 chars]         m  \n           e  \n           n  \n           t  '
  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  
?               -
+            t   : Expected different chart representation. Check that all spacing is exact.

----------------------------------------------------------------------
Ran 11 tests in 0.002s

FAILED (failures=1)

It’s propably a linebreak - you add one to x in the loop, even after the final letter.

1 Like

Finally works, thank you man

1 Like

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