# Python Budget App -- create\_spend\_chart function is killing me

**URL:** <https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425>\
**Category:** General\
**Created:** [July 23, 2020, 8:20pm UTC](https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425 "2020-07-23T20:20:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alchemixst](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alchemixst/32/137369_2.png) [@Alchemixst](https://forum.freecodecamp.org/u/Alchemixst)\
**Post date:** [July 23, 2020, 8:20pm UTC](https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425/1 "2020-07-23T20:20:39Z")

</div>

Hey all you smart, attractive people!

I’m hoping one of you can help me with this. As far as I can tell, the output of my function is identical to the test string ( **test\_create\_spend\_chart** for reference), but it’s not passing. If there are other ways to optimise the function(s) below, also, I’d be happy to hear about those as well!

```auto
### Category method
def get_withdrawls(self):
        total = 0
        for item in self.ledger:
            if item["amount"] < 0:
                total+= item["amount"]
        return total
###
def truncate(n):
    multiplier = 10
    return int(n * multiplier) / multiplier

def getTotals(categories):
    total = 0
    breakdown = []
    for category in categories:
        total += category.get_withdrawls()
        breakdown.append(category.get_withdrawls())
    
    #Breakdown of spending rounded down to nearest 10th
    rounded = list(map(lambda x: truncate(x/total), breakdown))
    return rounded

def create_spend_chart(categories):
    res = "Percentage spent by category\n"
    i = 100
    totals = getTotals(categories)
    while i >= 0:
        cat_spaces = " "
        for total in totals:
            if total * 100 >= i:
                cat_spaces += "o "
                #print(categories[totals.index(total)].name)
            else:
                cat_spaces += " "
        res+= str(i).rjust(3) + "|" + cat_spaces + ("\n")
        i-=10
    
    dashes = "-" + "---"*len(categories)
    names = []
    x_axis = ""
    for category in categories:
        names.append(category.name)

    maxi = max(names, key=len)

    for x in range(len(maxi)):
        nameStr = ' '
        for name in names:
            if x >= len(name):
                nameStr += " "
            else:
                nameStr += name[x] + " "
        nameStr += '\n'
        x_axis += nameStr

    res+= dashes.rjust(len(dashes)+4) + "\n" + x_axis
    return res

```

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![JuSa12](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jusa12/32/170508_2.png) [@JuSa12](https://forum.freecodecamp.org/u/JuSa12)\
**Post date:** [July 24, 2020, 2:11pm UTC](https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425/2 "2020-07-24T14:11:39Z")

</div>

Hey,  
i think the problem might be that you add a next line in the last row of nameStr.  
Not sure if it’s faster but you could change the while loop to a for loop which makes it easier to read in my opinion. for i in range(start, end, step)

---

<div class="post-metadata">

**Author:** ![Alchemixst](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alchemixst/32/137369_2.png) [@Alchemixst](https://forum.freecodecamp.org/u/Alchemixst)\
**Post date:** [July 24, 2020, 6:48pm UTC](https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425/3 "2020-07-24T18:48:58Z")

</div>

Thanks a lot! You just earned me my Python cert!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:45pm UTC](https://forum.freecodecamp.org/t/python-budget-app-create-spend-chart-function-is-killing-me/411425/4 "2021-06-01T18:45:29Z")

</div>

Closed on June 1, 2021, 6:45pm UTC


