Arithmetic_arranger - can't understand error codes

I am working on the Arithmetic Arranger problem and I don’t know what the errors I am getting actually mean or how to fix them. The core for loop works, but once I put into replit, I get weird errors that I can’t even understand.

import re

def arithmetic_arranger(problems, solve = False):
  
  if len(problems) > 5:
     return "Error: Too many problems."

  top = ""
  bottom = ""
  newDash = ""
  newSum = ""
  
  for problem in problems:
    
    if re.search("[*]", problem) or re.search("[/]", problem):
      return "Error: Operator must be '+' or '-'"
    if re.search("^/s[0-9].+-", problem):
      return "Error: Numbers must only contain digits"
    firstNumber = problem.split(' ')[0]
    operator = problem.split(' ')[1]
    secondNumber = problem.split(' ')[2]
    if len(firstNumber) >= 5 or len(secondNumber) >= 5:
      return "Error: Numbers cannot be more than four digits."
    
    sum =""
    if(operator == "+"):
        sum = str(int(firstNumber)+int(secondNumber))
    else:
        sum = str(int(firstNumber)-int(secondNumber))

    length = max(len(firstNumber), len(secondNumber)) +2
    topLine = str(firstNumber).rjust(length)
    bottomLine = operator + str(secondNumber).rjust(length-1)
    result = sum.rjust(length)
    dash = ""
    for y in range(length):
        dash += "-"

    if problem != problems[-1]:
      top += topLine + "    "
      bottom += bottomLine + "    "
      newDash += dash + "    "
      newSum += result + "    "
    else:
        top += topLine
        bottom += bottomLine
        newDash += dash
        newSum += result
  
  if solve:
    answer = (top + "\n" + bottom + "\n" + newDash + "\n" + newSum)
  else:
    answer = (top + "\n" + bottom + "\n" + newDash)
    
    return answer

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

Challenge: Arithmetic Formatter

Link to the challenge:

please share the tests output you get from the test suite


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hey just finishes the first python cert, a couple months ago. Most of the error codes are because the test output != your output. That is, your solution isn’t equal to what the test file wants to you get. Try to look like by line for the keyword “!=” or “does not equal”… see what you are suppose to be getting and how you can fix your code.

I still vividly remember, going line by line with the test code to make sure spaces, etc matched up, for each case.

If you are working offline, you can also download/copy the test solutions, then enter them into your code and compare them one by one. That is what I would recommend, as I did all the code offline first.

1 Like

Thanks for the advice! I actually figured it out but thank you!

2 Likes

Thanks, I forgot to add a period to the end of an error message and had to tweak my reg ex formatting and it works now!

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