# Scientific Computing with Python Projects - Arithmetic Formatter

**URL:** <https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [May 1, 2024, 3:22pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081 "2024-05-01T15:22:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnneuffer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lnneuffer/32/321416_2.png) [@lnneuffer](https://forum.freecodecamp.org/u/lnneuffer)\
**Post date:** [May 1, 2024, 3:22pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/1 "2024-05-01T15:22:10Z")

</div>

I do not know why my code does not pass the test cases for the show answers = false tests.

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        Errorlength = 'Error: Too many problems.'
        return Errorlength
    problems1=''
    problems2=''
    
    midline = ''
    for problem in problems:
        operand = problem.split(" ")
        if operand[1] not in ['+','-']:
            return "Error: Operator must be '+' or '-'."
        if not operand[0].isdigit() or not operand[2].isdigit():
            return 'Error: Numbers must only contain digits.'
        if len(operand[0]) > 4 or len(operand[2]) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        line = ''
    # Calculate the width needed for the problem
        width = max(len(operand[0]), len(operand[2])) + 2
        for lines in range(width):
            line += '-'
    # Add the formatted operands and operator to the arranged problems string
    
        problems1 += operand[0].rjust(width) + ' '
        problems2 += operand[1] + ' ' + operand[2].rjust(width - 2) + ' '
        midline += line.rjust(width) + ' '
        problems = problems1 + '\n' + problems2 + '\n' + midline + '\n'
        sproblems = ''.join(problems)
    # Remove trailing whitespace from the arranged problems string
        sproblems = sproblems.rstrip()

    # If show_answers is True, calculate and add the answers to the arranged problems
        if show_answers:
            problems += '\n'

            for problem in problems:
                operand[0], operand[1], operand[2] = problem.split()
                if operand[1] == '+':
                    answer = str(int(operand[0]) + int(operand[2]))
                else:
                    answer = str(int(operand[0]) - int(operand[2]))

                width = max(len(operand[0]), len(operand[2])) + 2
                problems += '-' * width + ' '
                problems += answer.rjust(width) + ' '

            sproblems = ' '.join(problems)
        # Remove trailing whitespace from the arranged problems string
            sproblems = sproblems.rstrip()

    # Return the arranged problems
    return sproblems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

```

---

<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 1, 2024, 3:24pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/2 "2024-05-01T15:24:59Z")

</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:** ![ios-man](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ios-man/32/337772_2.png) [@ios-man](https://forum.freecodecamp.org/u/ios-man)\
**Post date:** [May 1, 2024, 3:26pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/3 "2024-05-01T15:26:16Z")

</div>

🎈Hello @lnneuffer! Welcome to the forum!

It will be much easier to help you if you request help from the challenge step "Help " button as it will take the person helping you directly to the step in question.

Does this help?

Keep up the good progress!

Happy Coding! 🙂

---

<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 1, 2024, 3:28pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/4 "2024-05-01T15:28:18Z")

</div>

It looks like you have spacing issues, if you open the browser console you should see:

```diff
AssertionError: ' 11 3801 1 123 1 \n[91 chars]----' != ' 11 3801 1 123 1\n+ 4 -[69 chars]----'
- 11 3801 1 123 1     
? - - - - -----
+ 11 3801 1 123 1
- + 4 - 2999 + 2 + 49 - 9380     
? - - - - -----
+ + 4 - 2999 + 2 + 49 - 9380
- ---- ------ --- ----- ------
? - - - -
+ ---- ------ --- ----- ------

```

the lines starting with `-` are yours, the lines starting with `+` are the expected

---

<div class="post-metadata">

**Author:** ![lnneuffer](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lnneuffer/32/321416_2.png) [@lnneuffer](https://forum.freecodecamp.org/u/lnneuffer)\
**Post date:** [May 1, 2024, 6:13pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/5 "2024-05-01T18:13:24Z")

</div>

How do I open the browser console

---

<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 1, 2024, 6:20pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/6 "2024-05-01T18:20:33Z")

</div>

Google would tell you really easily, but F12

---

<div class="post-metadata">

**Author:** ![liamlee](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/liamlee/32/358626_2.png) [@liamlee](https://forum.freecodecamp.org/u/liamlee)\
**Post date:** [May 1, 2024, 6:22pm UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/7 "2024-05-01T18:22:51Z")

</div>

It looks like the issue lies in how the function handles the `show_answers` parameter. When `show_answers` is `False`, the function should only arrange the problems without calculating or displaying the answers. However, your code still calculates and adds the answers even when `show_answers` is `False`, which is likely causing the failed test cases for that condition.

To fix this, you should separate the logic for arranging the problems and calculating/displaying the answers based on the `show_answers` parameter. Make sure that when `show_answers` is `False`, the function only arranges the problems without calculating or displaying the answers.

---

<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:** [October 31, 2024, 6:23am UTC](https://forum.freecodecamp.org/t/scientific-computing-with-python-projects-arithmetic-formatter/688081/8 "2024-10-31T06:23:06Z")

</div>

Closed on October 31, 2024, 6:23am UTC

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