The assignment about arithmetic formatter

Hello everyone! first time coder here!

Can anyone please tell me why i keep getting the error from code? I had to transfer all the codes from replit to spyder to easily check it but still the correct code eludes me.

here is my code

def arithmetic_arranger(problems, solve = False):
  
  line_1 = ""
  line_2 = ""
  line_3 = ""
  line_4 = ""
  arranged_problems = ""
  #rule list
  #limit problems to 5
  if len(problems) > 5:
      return "Error: Too many problems."
  #problem arrangement, splitting into list
  
  for index, value in enumerate(problems):
      operation = value.split(" ")
    #array, splitting first and second value to the operand
      operand_1 = operation[0]
      operand_2 = operation[2]
      operator = operation[1]

    #checking operator
      if operator not in "-+":
          return "Error: Operator must be '+' or '-'."
   
     #checking value length
      if len(operand_1) > 4 or len(operand_2) > 4:
          return "Error: Numbers cannot be more than four digits."

    #checking value
      try:
          value_1 = int(operand_1)
          value_2 = int(operand_2)
      except ValueError:
          return "Error: Numbers must only contain digits."

    #operation
      sum = ""
      if(operator == "+"):
          sum = str(int(operand_1) + int(operand_2))
      elif(operator == "-"):
          sum = str(int(operand_1) - int(operand_2))

    #length of the operation
      length = max(len(operand_1), len(operand_2)) + 2
      top = str(operand_1).rjust(length)
      bot = operator + str(operand_2).rjust(length - 1)
      dash = ""
      answer = str(sum).rjust(length)

      #dictates the length of dashes
      for i in range(length):
          dash += "-"
      #output formatting
      if operation != problems[-1]:
          line_1 += top + '    '
          line_2 += bot + '    '
          line_3 += dash + '    '
          line_4 += answer + '    '
      else:
          line_1 += top
          line_2 += bot
          line_3 += dash
          line_4 += answer
  line_1.rstrip()
  line_2.rstrip()
  line_3.strip()
  if solve:
      line_4.strip()
      arranged_problems = line_1 + "\n" + line_2 + "\n" + line_3 + "\n" + line_4
  else:
      arranged_problems = line_1 + "\n" + line_2 + "\n" + line_3
    
  return arranged_problems

looking at the error, it seems like a line is inserted but i can’t find where it happens

AssertionError: Expected different output when calling "arithmetic_arranger()" with 
["3801 - 2", "123 + 49"]
E       assert '  3801      ...    -----    ' == '  3801      ...----    -----'

the difference here seens to be the spaces at the end in yours that are not there in the expected

ehhh? so does that mean the expected is on the left side while mine is on the right? I see, so i am checking it wrongly…
thanks!

yours is on the left, the expected on the right - you have extra spaces that are not expected

thanks, i got it! :slightly_smiling_face:
that resolved my confusion and i got the code right

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