Python Budget App: test_create_spend_chart fail

Tell us what’s happening:
For some reason, the categories that my chart prints are [Food, Clothing, Auto] instead of what it should print [Business, Food, Entertainment]. I’m not sure if this is because main.py creates a chart for [Food, Clothing, Auto] or if there’s something wrong with my code. I tried to test the code (printed the list of categories multiple times throughout) and it’s always correct until I run the entire thing. Not sure what’s wrong…

Your code so far

def create_spend_chart(categories):
  cates = []
  spending = []
  spent = 0
  #Creates a list of category names + spending per category
  for category in categories:
    cates.append(category.cate)
    for item in category.ledger:
      spent = 0
      if item["amount"] < 0:
        spent += int(item["amount"])
    spending.append(spent)
  total = sum(spending)
  #Changes amounts in spending to percents
  for num in spending:
    spending[spending.index(num)] = int((num / total) * 10) * 10
  #Length of longest string in cates
  longest = len(max(cates, key=len))  
  #Starts putting together output strings
  title = "Percentage spent by category" + "\n"
  #Sets length of each category to max
  for cate in cates:
    cates[cates.index(cate)] = cate.ljust(longest)
  #Prints all categories in a vertical line
  cateString = "     "
  for i in range(0, longest):
    for cate in cates:
      cateString += cate[i] + "  " #Two spaces between each category
    cateString += "\n     " #Categories start 5 spaces away from edge
  graph = ""
  for i in range(100, -10, -10):
    bar = " "
    line = str(i).rjust(3) + "|"
    #Add o if percent spending reaches there, spaces otherwise
    for percent in spending:
      if percent >= i:
        bar += "o  "
      else:
        bar += "   "
    line += bar
    graph += line + "\n"
    dashes = "    " + (len(cates) * 3) * "-" + "-" +  "\n"
    output = title + graph + dashes + cateString
  return(output)

Your browser information:

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

Challenge: Budget App

Link to the challenge:

When run main.py creates chart with [Food, Clothing, Auto], which is printed out. Later tests are run, which includes [Business, Food, Entertainment] as categories. Chart from these tests isn’t printed out, that’s all normal. Are you getting some error or just was confused with this behavior?

When I run the program, the last test fails. This is the error I get:

The part that shows what exactly differs from the expected result and actual function output is cut on the screen… My guess is that there’s somewhere one space too many or not enough. It might be easier if you paste full output with error, or just link to your code on repl.it.

Sorry about that, I updated the picture.

This code below works. I am a beginner. I know it may not be the best way, but it solves the problem.

    for i in range(0, longest):
        if i < longest - 1:
            for cate in cates:
                cateString += cate[i] + "  "
            cateString += "\n     "
        else:
            cateString += "      " + cate[longest-1]+ "  "

This is the exact same problem that I have, where you able to solve it?