# Help... Python Arithmetic Formatter

**URL:** <https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [July 9, 2020, 6:37am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647 "2020-07-09T06:37:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![santhika](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santhika/32/167879_2.png) [@santhika](https://forum.freecodecamp.org/u/santhika)\
**Post date:** [July 9, 2020, 6:37am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/1 "2020-07-09T06:37:56Z")

</div>

**Tell us what’s happening:**  
always failure on formatting output… can you help me to see what’s wrong with my code

**Your code so far**

```auto
def arithmetic_arranger(problems, statprint=False):
    # check problem list
    first = ''
    second = ''
    sumx = ''
    lines = ''
    # maximal problems is 5
    if len(problems) >= 6:
        return 'Error: Too many problems.'
    # split problem to separate components
    for i in problems:
        a = i.split()
        firsts = a[0]
        seconds = a[2]
        operands = a[1]
        # check the length of the number, max 4 digits
        if (len(firsts) > 4 or len(seconds) > 4):
            return "Error: Numbers cannot be more than four digits."

        # check the input as valid digits
        if not firsts.isnumeric() or not seconds.isnumeric():
            return "Error: Numbers must only contain digits."

        if (operands == '+' or operands == '-'):
            if operands == "+":
                sums = str(int(firsts) + int(seconds))
            else:
                sums = str(int(firsts) - int(seconds))
            # set length of sum and top, bottom and line values
            length = max(len(firsts), len(seconds)) + 2
            top = str(firsts).rjust(length)
            bottom = operands + str(seconds).rjust(length - 1)
            line = ''
            res = str(sums).rjust(length)
            for s in range(length):
                line += '-'
            # add to the overall string
            first += top + ' '
            second += bottom + ' '
            lines += line + ' '
            sumx += res + ' '
        else:
            return "Error: Operator must be '+' or '-'."
    # strip out spaces to the right of the string
    first.rstrip()
    second.rstrip()
    lines.rstrip()
    if statprint:
        sumx.rstrip()
        arranged_problems = first + '\n' + second + '\n' + lines + '\n' + sumx
    else:
        arranged_problems = first + '\n' + second + '\n' + lines
    return arranged_problems

```

**Here the output**

```auto
   32 3801 45 123    
+ 698 - 2 + 43 + 49    
----- ------ ---- -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [35 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123    
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49    
? ----
+ + 855 - 2 + 43 + 49
- ----- ------ ---- ----- ? ----
+ ----- ------ ---- ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: ' 3[23 chars] 123 \n- 698 - 3801 + 43 + 49 [73 chars] ' != ' 3[23 chars] 123\n- 698 - 3801 + 43 + 49\n-----[57 chars] 172'
- 32 1 45 123    
? ----
+ 32 1 45 123
- - 698 - 3801 + 43 + 49    
? ----
+ - 698 - 3801 + 43 + 49
- ----- ------ ---- -----    
? ----
+ ----- ------ ---- -----
- -666 -3800 88 172 ? ----
+ -666 -3800 88 172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.012s

FAILED (failures=2)

```

**Your browser information:**

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

**Challenge:** 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:** ![santhika](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santhika/32/167879_2.png) [@santhika](https://forum.freecodecamp.org/u/santhika)\
**Post date:** [July 9, 2020, 7:05am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/2 "2020-07-09T07:05:53Z")

</div>

solve after add some conditional statement on line 38

```auto
# add to the overall string
            if i != problems[-1]:
              first += top + ' '
              second += bottom + ' '
              lines += line + ' '
              sumx += res + ' '
            else:
              first += top
              second += bottom
              lines += line
              sumx += res

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [July 9, 2020, 7:22am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/3 "2020-07-09T07:22:11Z")

</div>

> [@santhika](#):
>
> `123 \n`

you have spaces before the new line character.

> [@santhika](#):
>
> `' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [35 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'`

yours is the `123 \n`, the expected one is `123\n`

---

<div class="post-metadata">

**Author:** ![eliseo324](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/eliseo324/32/167959_2.png) [@eliseo324](https://forum.freecodecamp.org/u/eliseo324)\
**Post date:** [July 9, 2020, 3:56pm UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/4 "2020-07-09T15:56:01Z")

</div>

I had a same problem. Thanks for your solution

---

<div class="post-metadata">

**Author:** ![brandonwie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/brandonwie/32/179890_2.png) [@brandonwie](https://forum.freecodecamp.org/u/brandonwie)\
**Post date:** [September 22, 2020, 6:22am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/5 "2020-09-22T06:22:38Z")

</div>

FYI, you can remove `rstrip()` after applying @santhika’s solution. I’m a beginner so don’t know exactly why rstrip doesn’t do the job, but negative index does, if anyone knows please leave a comment down below. Thanks @santhika again

---

<div class="post-metadata">

**Author:** ![jeevithajeev](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeevithajeev/32/105081_2.png) [@jeevithajeev](https://forum.freecodecamp.org/u/jeevithajeev)\
**Post date:** [December 4, 2020, 9:53am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/6 "2020-12-04T09:53:44Z")

</div>

Please help me resolve errors in below code:

```auto
def arithmetic_arranger(problems, option=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    line_1 = line_2 = line_3 = line_4 = ' '
    for i, problem in enumerate(problems):
        n1, operator, n2 = problem.split()
        number_1, number_2 = len(n1), len(n2)
        max_space = max(number_1, number_2)
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        if not (n1.isdigit()) and n2.isdigit():
            return "Error: Numbers must only contain digits."
        if number_1 > 4 and number_2 > 4:
            return "Error: Numbers cannot be more than four digits."

        result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)

        line_1 = line_1 + f"{n1:>2}"
        line_2 = line_2 + operator + f"{n2:>1}"
        line_3 = line_3 + '' + f"{'-':>1}"
        line_4 = line_4 + str(result).rjust(max_space + 2)

        if i < len(problems) - 1:
            line_1 += ' '
            line_2 += ' '
            line_3 += ' '
            line_4 += ' '

        if option:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}, \n{line_4}"
        else:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}"
        return arranged_problems

```

---

<div class="post-metadata">

**Author:** ![andrewcooper81](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewcooper81/32/205946_2.png) [@andrewcooper81](https://forum.freecodecamp.org/u/andrewcooper81)\
**Post date:** [February 16, 2021, 7:08am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/7 "2021-02-16T07:08:25Z")

</div>

Can someone help me check where my code went wrong please?  
I keep getting failures on test\_solutions and test\_arrangement

```auto
def arithmetic_arranger(problems, show = False):
  firstAnswer = ""
  operators = ""
  dashlines = ""
  secondAnswer = ""
  sumup = ""
  answer = ""

  if(len(problems))>5:
    return "Error: Too many problems."

  for problem in problems:
    #spliting the strings into a list
    firstNumber = problem.split(" ")[0]
    operators = problem.split(" ")[1]
    secondNumber = problem.split(" ")[2]

    # check the length of the number, max 4 digits
    if (len(firstNumber) > 4 or len(secondNumber) > 4):
      return "Error: Numbers cannot be more than four digits."

    # check the input as valid digits
    if not firstNumber.isnumeric() or not secondNumber.isnumeric():
      return "Error: Numbers must only contain digits."

    # check for the correct form of operators
    if operators != "+" or "-":
      return "Error: Operator must be '+' or '-'."

    sum = ""
    if operators == "+": 
      sum = str(int(firstNumber) + int(secondNumber))
    elif operators == "-":
      sum = str(int(firstNumber) - int(secondNumber))

    length = max(len(firstNumber) , len(secondNumber)) + 2
    fisrtline = str(firstNumber).rjust(length)
    secondline = operators + str(secondNumber).rjust(length - 1) 
    sltn = str(sum.rjust(length))

    dashline = ""
    for dash in range(length):
      dashline += "-"

    if problem != problems[-1]: 
      firstAnswer += fisrtline + " "
      secondAnswer += secondline + " "
      dashlines += dashline + " "
      sumup += sltn + " "
    else: 
      firstAnswer += fisrtline
      secondAnswer += secondline 
      dashlines += dashline 
      sumup += sltn 

  if show: 
    answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines + "\n" + sumup
  else:
    answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines    
  return answer

```

Here is the output:

```auto
 python main.py
Error: Operator must be '+' or '-'.
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FCC-Python-Project-10/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: "Error: Operator must be '+' or '-'." != ' 3 3801 45 123\n+ 855 [56 chars]----'
- Error: Operator must be '+' or '-'.
+ 3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FCC-Python-Project-10/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: "Error: Operator must be '+' or '-'." != ' 32 1 45 123\n- 698 [90 chars] 172'
- Error: Operator must be '+' or '-'.
+ 32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
 -666 -3800 88 172
 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=2)

```

---

<div class="post-metadata">

**Author:** ![andrewcooper81](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewcooper81/32/205946_2.png) [@andrewcooper81](https://forum.freecodecamp.org/u/andrewcooper81)\
**Post date:** [February 16, 2021, 7:15am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/8 "2021-02-16T07:15:26Z")

</div>

Here’s the link to my repl for easier dubugging [https://repl.it/@Itsvu81/FCC-Python-Project-10#arithmetic\_arranger.py](https://repl.it/@Itsvu81/FCC-Python-Project-10#arithmetic_arranger.py)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 16, 2021, 8:11am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/9 "2021-02-16T08:11:25Z")

</div>

@andrewcooper81 @jeevithajeev

please create your own topic when you ask for help on a challenge, thank you

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 16, 2021, 8:11am UTC](https://forum.freecodecamp.org/t/help-python-arithmetic-formatter/408647/10 "2021-02-16T08:11:28Z")

</div>


