# Scientific Computing with Python Projects - Arithmetic Formatter

**URL:** <https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [May 2, 2024, 12:45am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157 "2024-05-02T00:45:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![osmanuluc12](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/osmanuluc12/32/287813_2.png) [@osmanuluc12](https://forum.freecodecamp.org/u/osmanuluc12)\
**Post date:** [May 2, 2024, 12:45am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/1 "2024-05-02T00:45:34Z")

</div>

### Tell us what’s happening:

I have completed all tests manually, and the outputs seem correct. However, I don’t understand what’s wrong.

### Your code so far

```py
def arithmetic_arranger(problems, show_answers=False):
	
    list_operand1 = []
    list_operand2 = []
    list_result = []
    list_operator = []
    
    for problem in problems:
        problem = problem.split()
        if not problem[0].isdigit() or not problem[2].isdigit():
        	return print('Error: Numbers must only contain digits.')
    for problem in problems:
        problem = problem.split()    
        operand1 = int(problem[0])
        operand2 = int(problem[2])
        operator = problem[1]
        if len(problems) > 5:
            return print('Error: Too many problems.')
       	elif operand1 > 9999 or operand2 > 9999:
            return print('Error: Numbers cannot be more than four digits.')        
        elif show_answers == False:
        	result = " "
        elif show_answers == True and operator == '+':
            result = operand1 + operand2
        elif show_answers == True and operator == '-':
            result = operand1 - operand2
        elif operator != "+" or operator != "-":
            return print("Error: Operator must be '+' or '-'.")         
       	list_operand1.append(operand1)
        list_operand2.append(operand2)
        list_result.append(result)
        list_operator.append(operator)
    line1 = ""
    line2 = ""
    string = ""
    line3 = ""    
    for i in range(len(list_operand1)):
      loop1=len(str(list_operand1[i]))
      loop2=len(str(list_operand2[i]))
      loop3=len(str(list_result[i]))
      size = max(loop1, loop2)
      line1=line1 + " "*(2+size-loop1)+str(list_operand1[i])+ " " * 4
      line2=line2 + list_operator[i] + " "*(1 + size - loop2) + str(list_operand2[i]) + " "*4
      string=string + "-" * (size + 2) + " " * 4
      line3 =line3 + " " * (2 + size - loop3) + str(list_result[i]) + " " * 4
        
    return print(f'\n{line1}\n{line2}\n{string}\n{line3}')
   

arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

```

### Your browser information:

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

### Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter  
[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:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [May 2, 2024, 1:07am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/2 "2024-05-02T01:07:27Z")

</div>

Can you please share more information?

What errors are you getting?  
What tests are you not passing?  
What’s the output?

---

<div class="post-metadata">

**Author:** ![osmanuluc12](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/osmanuluc12/32/287813_2.png) [@osmanuluc12](https://forum.freecodecamp.org/u/osmanuluc12)\
**Post date:** [May 2, 2024, 1:49am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/3 "2024-05-02T01:49:21Z")

</div>

None of them:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/e/e/8/ee87bec937ec4dae9626f43df9d45a6745b2b3a8.png)

But if I call function manually:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/a/9/d/a9db65b089b765e321502214c9c27d06c739d25b.png)  
 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/a/b/4/ab49c4ff8ed86b54d483d1bada37da0c3a769ecc.png)

It seems like all of them should pass. I can see outputs clearly.

Tests:

- `arithmetic_arranger(["3801 - 2", "123 + 49"])` should return ` 3801 123\n- 2 + 49\n------ -----`.

- Failed:`arithmetic_arranger(["1 + 2", "1 - 9380"])` should return ` 1 1\n+ 2 - 9380\n--- ------`.

- Failed:`arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])` should return ` 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----`.

- Failed:`arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])` should return ` 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------`.

- Failed:`arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])` should return `'Error: Too many problems.'`.

- Failed:`arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])` should return `"Error: Operator must be '+' or '-'."`.

- Failed:`arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])` should return `'Error: Numbers cannot be more than four digits.'`.

- Failed:`arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])` should return `'Error: Numbers must only contain digits.'`.

- Failed:`arithmetic_arranger(["3 + 855", "988 + 40"], True)` should return ` 3 988\n+ 855 + 40\n----- -----\n 858 1028`.

- Failed:`arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)` should return ` 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028`.

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [May 2, 2024, 2:17am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/4 "2024-05-02T02:17:24Z")

</div>

Your function is failing all of the tests because it isn’t really returning the answers, it’s returning a `print()`

```auto
return print(f'\n{line1}\n{line2}\n{string}\n{line3}')

```

Don’t worry about printing the result in the function, the function should return only the result.

If you want to print it, print the function call like this:

```auto
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))

```

If you need to troubleshoot more after that add `repr()` to see the raw string output:

```auto
print(repr(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)))

```

---

<div class="post-metadata">

**Author:** ![osmanuluc12](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/osmanuluc12/32/287813_2.png) [@osmanuluc12](https://forum.freecodecamp.org/u/osmanuluc12)\
**Post date:** [May 2, 2024, 3:19pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/5 "2024-05-02T15:19:09Z")

</div>

I changed my code. Now, it is passing the all error tests. But it can not pass other tests.

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/9/1/c/91caf011887fa1f60277ca22332142af21859f68.png)

For the test above, output is :  
 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/5/a/2/5a21574cdd02dc3adfd6f9ad51591f07716a7108.png)

It seems correct but I am not sure about it.  
Should I get rid of the ‘\n’ or f-string on the beginning of last return?  
return f’\n{line1}\n{line2}\n{string}\n{line3}’

By the way if I remove the ‘\n’, output seems like this:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/8/2/d/82d5272084067297936a6d2e3654139d545dd9ae.png)

---

<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:** [May 2, 2024, 3:26pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/6 "2024-05-02T15:26:33Z")

</div>

if you want more help you need to post your updated code

> [@osmanuluc12](#):
>
> Should I get rid of the ‘\n’ or f-string on the beginning of last return?  
> return f’\n{line1}\n{line2}\n{string}\n{line3}’

yes, the final output doesn’t require a newline at the beginning, if you want to have it put it in your print

---

<div class="post-metadata">

**Author:** ![pkdvalis](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pkdvalis/32/350571_2.png) [@pkdvalis](https://forum.freecodecamp.org/u/pkdvalis)\
**Post date:** [May 2, 2024, 3:37pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/7 "2024-05-02T15:37:11Z")

</div>

Just focus on passing the first test first.

Use `repr()` to compare your output to the expected output.

```auto
print(repr(arithmetic_arranger( ["3801 - 2", "123 + 49"])))

```

should return

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

```

This will show the spacing and the newline characters.

---

<div class="post-metadata">

**Author:** ![osmanuluc12](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/osmanuluc12/32/287813_2.png) [@osmanuluc12](https://forum.freecodecamp.org/u/osmanuluc12)\
**Post date:** [May 6, 2024, 4:14pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/8 "2024-05-06T16:14:21Z")

</div>

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return 'Error: Too many problems.'

    line1, line2, line3, line4 = "", "", "", ""
    for problem in problems:
        operand1, operator, operand2 = problem.split()

        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."

        if not (operand1.isdigit() and operand2.isdigit()):
            return 'Error: Numbers must only contain digits.'

        if len(operand1) > 4 or len(operand2) > 4:
            return 'Error: Numbers cannot be more than four digits.'

        width = max(len(operand1), len(operand2)) + 2

        line1 += operand1.rjust(width) + " "
        line2 += operator + operand2.rjust(width - 1) + " "
        line3 += '-' * width + " "

        if show_answers:
            result = str(eval(problem))
            line4 += result.rjust(width) + " "

    arranged_problems = "\n".join([line1.rstrip(), line2.rstrip(), line3.rstrip(), line4.rstrip()])
    return arranged_problems
   

print(arithmetic_arranger(["3801 - 2", "123 + 49"]))

```

I changed my code like this. It is passing some of the tests, but I cannot understand what’s wrong with the code.

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/1/6/a/16aad777cf54597e373c915997802fde46731255.png)

And I tried my code on a different platform:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/b/c/f/bcf67eb5559ea2024857c53bf123beded8f8a696.png)

It seems perfect. However, on the freeCodeCamp preview board, there are three greater-than signs (\>) and it is ruining my output. Why are you including these as default?

---

<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:** [May 6, 2024, 4:26pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/9 "2024-05-06T16:26:22Z")

</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:** ![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:** [May 6, 2024, 4:27pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/10 "2024-05-06T16:27:55Z")

</div>

```auto
AssertionError: ' 3801 123\n- 2 + 49\n------ -----\n' != ' 3801 123\n- 2 + 49\n------ -----'
    3801 123
  - 2 + 49
- ------ -----
? -
+ ------ -----

```

you have an extra new line character at the end of last line

---

<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:** [November 5, 2024, 4:28am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688157/11 "2024-11-05T04:28:54Z")

</div>

Closed on November 5, 2024, 4:28am UTC

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