Problem with the solution of budget app

Tell us what’s happening:
Hello everyone and thanks for any suggestions!
I have finished the app budget test, but I get an error that I can’t understand, because the final chart looks exactly the same as the one in the example…
Here is my code:

Your code so far

class Category:

    CATEGORIES = dict() # verrà aggiornata solo con i prelievi di ogni categoria
    
    def __init__(self, cat):
        self.cat = cat
        self.ledger = [] 
        self.saldo = 0
        self.CATEGORIES.update({self.cat:0}) # aggiunge ogni nuova categoria a CATEGORIES


    def __repr__(self):
        self.stampa = f'{self.cat:*^30}\n'

        for x in self.ledger:
            self.stampa += f"{x['description'][:23]:<23}{float(x['amount']):>7.2f}\n" \
                           if not isinstance(x['amount'], str) else f"{x['description'][:23]:<23}\n"

        return self.stampa + 'Total: ' + str(self.saldo)

  
    def get_balance(self):
        return self.saldo


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


    def deposit(self, dep, description = ''):
        self.ledger.append({'amount':dep, 'description':description})
        self.saldo += dep


    def withdraw(self, wtd, description = ''):
        if self.check_funds(wtd):
        
            self.ledger.append({'amount':-wtd, 'description':description})
            self.saldo -= wtd
            self.CATEGORIES[self.cat] += wtd # aggiunge il prelievo in CATEGORIES

            return True
        return False


    def transfer(self, trf, other):
        if self.check_funds(trf):
     
            self.ledger.append({'amount':-trf, 'description':f'Transfer to {other.cat}'})
            self.saldo -= trf
            self.CATEGORIES[self.cat] += trf # aggiunge il trasferimento in CATEGORIES

            other.ledger.append({'amount':trf, 'description':f'Transfer from {self.cat}'})
            other.saldo += trf

            return True
        return False
            

def create_spend_chart(categories):
    tot = {}
    n = 100

    title = 'Percentage spent by category\n' 
    div = '    ' + '---' * len(categories) + '-'
    
    # aggiorna tot con gli elementi inseriti nella lista categories
    for item in categories:
        if item.cat in Category.CATEGORIES:
            tot[item.cat] = Category.CATEGORIES[item.cat]

    num = sum(tot.values())

    # per ogni decina da 100 a zero aggiunge spazi oppure 'O'
    # a seconda del valore e poi aggiunge la linea finale 
    while n >= 0:
        spc = ''
        for voce in tot:
            spc += ' o ' if (tot[voce] / num * 100) >= n else '   '
        title += f"{n:>3}|{spc}\n"
        n -= 10 
    title += div + '\n'

    # stampa i nomi delle categorie al fondo del grafico
    names_cat = tot.keys()    
    max_len = max(names_cat, key=len)

    # inserisce i nomi delle categorie alla fine del grafico
    for i in range(len(max_len)):
        spc_str = "    "
        for name in names_cat:
            spc_str += '   ' if i >= len(name) else f' {name[i]} ' 

        if i != len(max_len)-1:
            spc_str += '\n'
       
        title += spc_str

    return title

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Budget App

Link to the challenge:

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

Here is the link to my replit:
boilerplate-budget-app - Replit

Thank you!

'Perc[34 chars]         \n 80|         \n 70|    o[316 chars]  t '
!=
'Perc[34 chars]          \n 80|          \n 70|  [340 chars] t  '

Here is the error message - top line is your output, bottom line is expected output. So basically, you chart is missing one space per line.

Adding one space per line the error message is:

FAIL: test_create_spend_chart (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app/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[34 chars] \n\n 90| \n\n 80| \n\n 70[362 chars] t ’ != 'Perc[34 chars] \n 90| \n 80| \n 70| [340 chars] t ’
Diff is 1316 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.019s

'Perc[34 chars]     \n 90|         \n 80|         \n 70|    o[316 chars]  t '
'Perc[34 chars]      \n 90|          \n 80|          \n 70|  [340 chars] t  '

There is still 9 spaces in your line but 10 in the expected line.

I solved … now it’s ok !
thanks !

1 Like

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