Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

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

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

Welcome to the forum.

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

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

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

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

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?

No, you need return

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

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?

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

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?

          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"])

What is your function returning?

Please share your full code as well.

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 :grimacing:

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"])

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

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.

Like this? Nothing is showing on console :frowning:

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"])}')

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.geeksforgeeks.org/python-functions/

This is something you want a solid understanding of

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

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"])}')

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

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

all this code is not in the function
code
code
code

It’s not indented.

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

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

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?

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"])}')

Yes this is looking a lot better :+1:

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

How can you find out?