PYTHON BUDGET APP- create_spend_chart

I have created almost identical chart according to But still it is giving me a fail. I can’t find the difference what am I doing wrong. I also looked in the forum regarding this challenge and learned from this solution = [Python Budget App -- create_spend_chart function is killing me]

Your code so far

def create_spend_chart(categories):
    cat_totals = []
    for cat in categories:
        total = 0
        for c in cat.ledger:
            if c['amount'] < 0:
                total += c['amount']
    perc_totals = [int(x/sum(cat_totals) * 10)/10 for x in cat_totals]
    i = 100
    res = 'Percentage spent by category' + '\n'
    while i >= 0:
        spc = ' '
        for value in perc_totals:
            if value * 100 >= i:
                spc += 'o  '
                spc += '   '
        if i == 0:
            res += str(i).rjust(3) + '|' + spc
            res += str(i).rjust(3) + '|' + spc + '\n'
        i -= 10
    #need categories name
    cat_names = [x.category for x in categories]
    dashes = '-' + '---' * len(cat_names)
    res += '\n' + ' ' * 4 + dashes.rjust(3)
    maxi = len(max(cat_names, key = len))
    names = ' ' * 4
    for i in range(maxi):
        for name in cat_names:
                names += ' ' + name[i] + ' '
            except IndexError:
                names += '   '  
        if i == maxi - 1:
          names += ' ' * 4
          names +=  '\n' + ' ' * 4 
    res += '\n' + names
    return res

And can string transposition help?

Challenge: Budget App

Link to the challenge:

Please provide the error message. We’ll be able to help much faster that way.

hello, this appear to be a common problem, i also had hard time making my chart match the expected output. Just like you I browsed different forum posts, which involved the same issue.
The way i managed to fix it, i looked up the tests file, there you can find what the expected output from the chart test is. Its under the form of a string. I compared that to the string my own chart function returns, using the test parameters. Looking it this way, it makes it very clear where differences occur. Iirc, at the end of every line there was an extra space, so i had to look at my code, where those lines were generated and edited with an extra space. Hope that helps.

1 Like

Yeah I compared the length of the expected output and there is some extra spacing. I’ll try to sort that out. Thanks for the help!

FAIL: test_create_spend_chart (test_module.UnitTests)
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app-5/", line 94, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: 'Perc[226 chars]F  E \n     u  o  n \n     s  o  t \n     i  d[139 chars]    ' != 'Perc[226 chars]F  E  \n     u  o  n  \n     s  o  t  \n     i[148 chars] t  '
Diff is 891 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.

From this error I wasn’t understanding the problem until I compared the length of both strings.

1 Like

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