# Scientific Computing with Python Projects - Arithmetic Formatter

**URL:** <https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [February 27, 2023, 4:38pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027 "2023-02-27T16:38:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![demo\_star](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@demo\_star](https://forum.freecodecamp.org/u/demo_star)\
**Post date:** [February 27, 2023, 4:38pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027/1 "2023-02-27T16:38:36Z")

</div>

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

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**

```auto
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:**  
[https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter](https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 27, 2023, 4:40pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027/2 "2023-02-27T16:40:55Z")

</div>

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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [February 27, 2023, 5:03pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027/3 "2023-02-27T17:03:45Z")

</div>

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

```auto
  3801 123
- 2 + 49
------ -----

```

Your function returns:

```auto
  3801 123
- 2 + 49
------ -----

```

---

<div class="post-metadata">

**Author:** ![demo\_star](https://avatars.discourse-cdn.com/v4/letter/d/d07c76/32.png) [@demo\_star](https://forum.freecodecamp.org/u/demo_star)\
**Post date:** [February 27, 2023, 5:20pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027/4 "2023-02-27T17:20:48Z")

</div>

Yes! 10 passed! Many thanks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 29, 2023, 5:21am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/595027/5 "2023-08-29T05:21:04Z")

</div>

Closed on August 29, 2023, 5:21am UTC

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