Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Hello, I’m not sure what’s wrong with my code, 4 passed and 6 failed :frowning:

test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]
Your code so far

import re

def arithmetic_arranger(problems, result = False):
    if(len(problems) > 5):
      return "Error: Too many problems."
    
    x = ""
    y = ""
    lines = ""
    sumx = ""
    string = ""

    for problem in problems:
      if(re.search("[^\s0-9.+-]", problem)):
        if(re.search("[/]", problem) or re.search("[*]", problem)):
          return "Error: Operator must be '+' or '-'."
        return "Error: Numbers must only contain digits."

      firstNum = problem.split(" ")[0]
      operator = problem.split(" ")[1]
      secondNum = problem.split(" ")[2]

      if(len(firstNum) >= 5 or len(secondNum) >=5):
        return "Error: Numbers cannot be more than four digits."

      sum =""
      if(operator == "+"):
        sum = str(int(firstNum) + int(secondNum))
      elif(operator == "-"):
        sum = str(int(firstNum) - int(secondNum))

      length = max(len(firstNum), len(secondNum)) + 2
      top = str(firstNum).rjust(length)
      bottom = operator + str(secondNum).rjust(length - 1)
      line = ""
      res = str(sum).rjust(length)
      for s in range(length):
        line += "-"

      if problem != problems[-1]:
        x += top + ' '
        y += bottom + ' '
        lines += line + ' '
        sumx += res + ' '
      else:
        x += top
        y += bottom
        lines += line
        sumx += res

    if result:
      string = x +"\n"+ y +"\n"+ lines +"\n"+ sumx
    else:
      string = x +"\n"+ y +"\n"+ lines
    return string

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

I’ve edited your code 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 (').

For arithmetic_arranger(['3801 - 2', '123 + 49']), the expected return value:

  3801      123
-    2    +  49
------    -----

Your function returns:

  3801   123
-    2 +  49
------ -----
1 Like

Yes! 10 passed! Many thanks