Getting error in challenge Arithmetic Formatter

I don’t know why it is throwing errors when testing in replit, I’ve tried it on visual code and gives the same result as the expected result (I copied the expected_output string and printed it)

Your code so far

def arithmetic_arranger(problems, visiblity = False):
  result = ""
  problem_top = ""
  problem_bottom = ""
  problem_dash = ""
  problem_result = ""
  
  if len(problems)>5:
    return "Error: Too many problems."
    
  for problem in problems:
    problem = str(problem)
    problem = problem.split(" ")
  
    try:
      int(problem[0])
      int(problem[2])
    except:
      return "Error: Numbers must only contain digits."
    if problem[1] not in ["+","-"]:
      return "Error: Operator must be '+' or '-'."
    if len(problem[0])>4 or len(problem[2])>4:
      return "Error: Numbers cannot be more than four digits."
      
    count_1 = len(problem[0])
    count_2 = len(problem[2])
    len_dash = max(count_1,count_2) + 2
    
    if problem[1] == "+": 
      result = str(int(problem[0]) + int(problem[2]))
    else:
      result = str(int(problem[0]) - int(problem[2]))
      
    if problem != problems[-1]:
      problem_top += str(problem[0]).rjust(len_dash) + "    "
      problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1) + "    "
      for r in range(len_dash):
        problem_dash += "-"
      problem_dash += "    "
      problem_result += str(result).rjust(len_dash) +"    "
    else:
      problem_top += str(problem[0]).rjust(len_dash)
      problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1)
      for r in range(len_dash):
        problem_dash += "-"
      problem_result += str(result).rjust(len_dash)
      
  if visiblity==True:
    arranged_problems = (problem_top + "\n" + problem_bottom + "\n" + problem_dash + "\n" + problem_result )
  else:
    arranged_problems = (problem_top + "\n" + problem_bottom + "\n" + problem_dash )
    
  return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Arithmetic Formatter

Link to the challenge:

What errors exactly are you getting? Usually the output gives information at what to look.

this is what it is showing, the code is failing to meet the output. and I don’t know what is wrong

Take a look at the line with ? and ++++ in the error output. In this case ++++ indicates specific place where difference between expected line and line returned by function occurs. It looks at the end of each line there’s added few spaces, that are not expected there.

1 Like

thanks it seems this was the case. I had already written a code to solve this problem, but it seems due to change in the local variable ‘problem’, it wasn’t doing the purpose. it was working good after I copied it into another variable

if problem != problems[-1]:
      problem_top += str(problem[0]).rjust(len_dash) + "    "
      problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1) + "    "
      for r in range(len_dash):
        problem_dash += "-"
      problem_dash += "    "
      problem_result += str(result).rjust(len_dash) +"    "
    else:
      problem_top += str(problem[0]).rjust(len_dash)
      problem_bottom += str(problem[1]) + str(problem[2]).rjust(len_dash-1)
      for r in range(len_dash):
        problem_dash += "-"
      problem_result += str(result).rjust(len_dash)

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