# Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

**URL:** <https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [February 24, 2025, 10:54am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383 "2025-02-24T10:54:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 24, 2025, 10:54am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/1 "2025-02-24T10:54:44Z")

</div>

### Tell us what’s happening:

Newbie trying to follow the rules to write the codes but it does not seem to work now? Checked indentation, it should be correct could anyone please help? T\_T

### Your code so far

```py
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print('Error: Too many problems.')
        
        for problem in problems:
            
            operand1, operand2, operator = problem.split()
            
            if operator != '+' and operator!= '-':
                return "Error: Operator must be '+' or '-'."

            elif not operand1.isdigit() or not operand2.isdigit():
                print('Error: Numbers must only contain digits.')

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

            top = []
            middle = []
            bottom = []
            answers = []

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

            top.append(operand1.rjust(max_len))
            middle.append(operator + ' ' + operand2.rjust(max_len))
            bottom.append('-' * max_len)

        if show_answers:
            if operator == '+':
                results = int(operand1) + int(operand2)
            else:
                results = int(operand1) - int(operand2)
                answers.append(results.rjust(max_len))
        else:
            answers.append(' ')

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

```

### Your browser information:

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

### Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/scientific-computing-with-python/build-an-arithmetic-formatter-project/build-an-arithmetic-formatter-project)**
>
> Learn to Code — For Free

---

<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:** [February 24, 2025, 12:18pm UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/2 "2025-02-24T12:18:58Z")

</div>

Welcome to the forum.

> [@stellahu](#):
>
> `return arithmetic_arranger(["3801 - 2", "123 + 49"])`

You call the function recursively in your `return` statement. There’s no exit condition here it will just call itself forever.

> [@stellahu](#):
>
> ```auto
> if operator != '+' and operator!= '-':
> return "Error: Operator must be '+' or '-'."
> 
> elif not operand1.isdigit() or not operand2.isdigit():
> print('Error: Numbers must only contain digits.')
> 
> ```

Keep in mind you need to `return` these messages not `print` them

---

<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 24, 2025, 6:09pm UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/3 "2025-02-24T18:09:42Z")

</div>

> [@stellahu](#):
>
> ` if len(problems) > 5:`

consider what is executed in this case, and what is executed when this doesn’t happen  
you have indentation issues

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 26, 2025, 10:14am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/4 "2025-02-26T10:14:41Z")

</div>

Thanks a lot. Doest it mean I should delete the return function?

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 26, 2025, 10:17am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/5 "2025-02-26T10:17:52Z")

</div>

when length is longer than 5, print error and then check all the conditions. when length is 1-5, do the math. hmm does it mean i should add an else clause to conclude the if else condition?

---

<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:** [February 26, 2025, 11:50am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/6 "2025-02-26T11:50:37Z")

</div>

No, you need `return`

---

<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 26, 2025, 1:56pm UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/7 "2025-02-26T13:56:32Z")

</div>

> [@stellahu](#):
>
> when length is 1-5, do the math.

are you sure? check your indentation, what is inside that `if` statement?

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 27, 2025, 8:44am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/8 "2025-02-27T08:44:48Z")

</div>

> [@ILM](#):
>
> consider what is executed in this case, and what is executed when this doesn’t happen  
> you have indentation issues

Okay I am trying very hard to read the code and understand the issue. Apology if I cannot realize that fast. So in the if statement I misput a bunch of restrictions that i should have checked on those length are not greater than 5 (which is 1-4), does it mean I should move the whole for loop out of if statement?

---

<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 27, 2025, 10:14am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/9 "2025-02-27T10:14:19Z")

</div>

do you want the loop to happen only when there are more than 5 poblems? if that so you can leave it where it is

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 27, 2025, 10:55am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/10 "2025-02-27T10:55:54Z")

</div>

> [@stellahu](#):
>
> > [@ILM](#):
> >
> > consider what is executed in this case, and what is executed when this doesn’t happen  
> > you have indentation issues

Yes! In this case I should move the whole correct format & showing answer out of if len \> 5, so that the correct answer shows right?

```auto
          top = []
            middle = []
            bottom = []
            answers = []

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

            top.append(operand1.rjust(max_len))
            middle.append(operator + ' ' + operand2.rjust(max_len))
            bottom.append('-' * max_len)

        if show_answers:
            if operator == '+':
                results = int(operand1) + int(operand2)
            else:
                results = int(operand1) - int(operand2)
                answers.append(results.rjust(max_len))
        else:
            answers.append(' ')

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

```

---

<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:** [February 27, 2025, 12:09pm UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/11 "2025-02-27T12:09:29Z")

</div>

What is your function returning?

Please share your full code as well.

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 28, 2025, 12:43am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/12 "2025-02-28T00:43:19Z")

</div>

I tried to move the answer part out of all the restriction loop and if condition. There might be something still wrong nothing showing at console 😬

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print('Error: Too many problems.')
        
        for problem in problems:
            
            operand1, operand2, operator = problem.split()
            
            if operator != '+' and operator!= '-':
                return "Error: Operator must be '+' or '-'."

            elif not operand1.isdigit() or not operand2.isdigit():
                print('Error: Numbers must only contain digits.')

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

    top = []
    middle = []
    bottom = []
    answers = []

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

    top.append(operand1.rjust(max_len))
    middle.append(operator + ' ' + operand2.rjust(max_len))
    bottom.append('-' * max_len)

    if show_answers:
        if operator == '+':
            results = int(operand1) + int(operand2)
        else:
            results = int(operand1) - int(operand2)
            answers.append(results.rjust(max_len))
    else:
        answers.append(' ')

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

```

---

<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:** [February 28, 2025, 12:49am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/13 "2025-02-28T00:49:38Z")

</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:** ![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:** [February 28, 2025, 12:51am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/14 "2025-02-28T00:51:14Z")

</div>

You have created a function called `arithmetic_arranger()`. You should call your function to test it, and print the result.

Nothing is showing in the console because you never call the function.

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 28, 2025, 3:33am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/15 "2025-02-28T03:33:28Z")

</div>

Like this? Nothing is showing on console ☹

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print('Error: Too many problems.')
        
        for problem in problems:
            
            operand1, operand2, operator = problem.split()
            
            if operator != '+' and operator!= '-':
                return "Error: Operator must be '+' or '-'."

            elif not operand1.isdigit() or not operand2.isdigit():
                print('Error: Numbers must only contain digits.')

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

    top = []
    middle = []
    bottom = []
    answers = []

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

    top.append(operand1.rjust(max_len))
    middle.append(operator + ' ' + operand2.rjust(max_len))
    bottom.append('-' * max_len)

    if show_answers:
        if operator == '+':
            results = int(operand1) + int(operand2)
        else:
            results = int(operand1) - int(operand2)
            answers.append(results.rjust(max_len))
    else:
        answers.append(' ')

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

```

---

<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:** [February 28, 2025, 3:36am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/16 "2025-02-28T03:36:37Z")

</div>

Yes, but it will never get executed because it’s indented to be inside the function definition.

I suggest you review how to make and call functions:

[https://www.w3schools.com/python/python\_functions.asp](https://www.w3schools.com/python/python_functions.asp)

[https://www.geeksforgeeks.org/python-functions/](https://www.geeksforgeeks.org/python-functions/)

This is something you want a solid understanding of

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [February 28, 2025, 11:52am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/17 "2025-02-28T11:52:50Z")

</div>

I have tried to move print function and add the final result, now console is showing File “main.py”, line 45  
SyntaxError: ‘return’ outside function no matter how I indent it.

If I comment the return final\_result first it will show File “main.py”, line 24, in   
NameError: name ‘operand1’ is not defined. Hmm seems like something is wrong up there as well

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return('Error: Too many problems.')
    
    for problem in problems:
            
        operand1, operand2, operator = problem.split()
            
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."

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

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

   
top = []
middle = []
bottom = []
answers = []
    
max_len = max(len(operand1), len(operand2)) + 2

top.append(operand1.rjust(max_len))
middle.append(operator + ' ' + operand2.rjust(max_len))
bottom.append('-' * max_len)

if show_answers:
    if operator == '+':
        results = int(operand1) + int(operand2)
    else:
        results = int(operand1) - int(operand2)
        answers.append(results.rjust(max_len))
else:
    answers.append(' ')
    

final_result = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(bottom)
    
if show_answers:
    final_result += '\n' + ' '.join(answers)

return final_result

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

```

---

<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:** [February 28, 2025, 11:55am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/18 "2025-02-28T11:55:41Z")

</div>

I see a lot of code that is not indented. That means it’s not part of the function.

```auto
def function:
    all this code is part of the function
    code
    code
    code

all this code is not in the function
code
code
code

```

> [@stellahu](#):
>
> SyntaxError: ‘return’ outside function

It’s not indented.

> [@stellahu](#):
>
> NameError: name ‘operand1’ is not defined.

`operand` is defined here, indented so it is part of the function.

> [@stellahu](#):
>
> ` operand1, operand2, operator = problem.split()`

It only exists **inside** the function. Here you use `operand1` outside the function (not indented) so it does not exist.

> [@stellahu](#):
>
> `max_len = max(len(operand1), len(operand2)) + 2`

---

<div class="post-metadata">

**Author:** ![stellahu](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stellahu](https://forum.freecodecamp.org/u/stellahu)\
**Post date:** [March 1, 2025, 1:20am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/19 "2025-03-01T01:20:16Z")

</div>

I see. Now I move function code inside function and call it from outside. Console only showing Error: Operator must be ‘+’ or ‘-’. It seems like it got stuck here even though there is only + - when I call function?

```auto
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return('Error: Too many problems.')
    
    for problem in problems:
            
        operand1, operand2, operator = problem.split()
            
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."

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

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

   
    top = []
    middle = []
    bottom = []
    answers = []
    
    max_len = max(len(operand1), len(operand2)) + 2

    top.append(operand1.rjust(max_len))
    middle.append(operator + ' ' + operand2.rjust(max_len))
    bottom.append('-' * max_len)

    if show_answers:
        if operator == '+':
            results = int(operand1) + int(operand2)
        else:
            results = int(operand1) - int(operand2)
            answers.append(results.rjust(max_len))
    else:
        answers.append(' ')
    

    final_result = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(bottom)
    
    if show_answers:
        final_result += '\n' + ' '.join(answers)

    return final_result

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

```

---

<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:** [March 1, 2025, 2:06am UTC](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383/20 "2025-03-01T02:06:24Z")

</div>

Yes this is looking a lot better 👍

It makes you wonder what value is in the `operator` variable right before that `if` statement is triggered.

How can you find out?

[Next page](https://forum.freecodecamp.org/t/build-an-arithmetic-formatter-project-build-an-arithmetic-formatter-project/736383.md?page=2)
