For loop return problem in Arithmetic Formatter

When ever I try to return the ‘arranged_problems’ i only get the final value from the arithmetic arranger list but when I use ‘print(arranged_problems)’ all the values are printed but with a ‘none’ in then end. Sorry if it’s a dumb question I am new to programming and what I have learned I can’t seem to find the issue

def arithmetic_arranger(problems, solve = False):
    if len(problems) > 5:
        return ("Error: Too many problems.")
    
    for m in (problems):
        x = m.split()

        if (x[0].isnumeric()) == False or (x[2].isnumeric()) == False:
            return ("Error: Numbers must only contain digits.")
        else:
            if len(x[0]) >= 5:
                return ("Error: Numbers cannot be more than four digits.")
            
            if len(x[2]) >= 5:
                return ("Error: Numbers cannot be more than four digits.")
            
            if x[1] == '*' or x[1] == '/':
                return ("Error: Operator must be '+' or '-'.")
            
            else:
                num1 = str(x[0])
                num2 = str(x[2])
                ans = ""

                if x[1] == '+':
                    ans = int(x[0]) + int(x[2])

                if x[1] == '-':
                    ans = int(x[0]) - int(x[2])

            length = max(len(num1), len(num2)) + 2
            top = num1.rjust(length)
            bottom = x[1] + num2.rjust(length - 1)
            line = ""
            fans = str(ans).rjust(length)
            for s in range(length):
                line += "-"
            if solve:
                arranged_problems = (top + "\n" + bottom + "\n" + line + "\n" + fans)

            else:
                arranged_problems = (top + "\n" + bottom + "\n" + line)

        return(arranged_problems)

print(arithmetic_arranger(["33 + 690", "3301 - 2", "45 + 3", "123 + 49"]))

It doesn’t seem like final is ever assigned a value.

sorry didn’t see that i changed it by mistake when i was posting the code here :sweat_smile: so that was not the problem and the problem still exists

In that case, it might be because you’re returning from the function on the first iteration of your loop, if the indentation you posted is actually what’s in your code. Can you link to your repl?

https://replit.com/@MOHAMMEDAL45/boilerplate-arithmetic-formatter#arithmetic_arranger.py

As I suspected, your assignment to arranged_problems and return(arranged_problems) are both indented improperly. There’s a (easily overlooked) squiggly red line after if solve telling you as much.

Woah thanks a lot I didn’t know that the squiggly line meant there is an indentation problem :sweat_smile: I just thought it is like WORD putting a redline under my name so I didn’t care much :grin:. now I have changed the indentation but it still only returns the first element of the loop and if I use the print function it prints all elements from loop.

You’ve changed the indentation in the wrong direction, now you’re returning after the first loop iteration.

If you’re finding it difficult to visually track indentation levels, collapse code blocks and see what disappears/remains (try clicking the square minus sign next to 5 in the line numbers).

After you sort out the indentation, your main problem is the lines like top = num1.rjust(length), where you (re)assign top every time through the loop instead of adding to it. So, you need to build up your lines problem by problem, with the correct spacing, and then add the correct line endings. Once you can see all the problems, adjusting the spacing is relatively easy.

Thanks a lot @andrew-1135 for helping me. as a person who’s new to programming It meant a lot and @jeremy.a.gray thanks for the advice and I took your advice and added new lines to the code and now it runs flawlessly. God bless u both :heart:

PS: both the suggestions where the solutions too my problem but i could only make one reply as solution so sry @jeremy.a.gray :sweat_smile:

Well done, here’s my solution for reference:

def arithmetic_arranger(problems: str, display_answers: bool = False) -> str:
    if len(problems) > 5:
        return "Error: Too many problems."

    arithmetic_problems = []

    for p in problems:
        operand1, operator, operand2 = p.split()

        operand_len = max(len(operand1), len(operand2))
        if operand_len > 4:
            return "Error: Numbers cannot be more than four digits."

        try:
            if operator == "+":
                answer = int(operand1) + int(operand2)
            elif operator == "-":
                answer = int(operand1) - int(operand2)
            else:
                return "Error: Operator must be '+' or '-'."
        except ValueError:
            return "Error: Numbers must only contain digits."

        problem_width = operand_len + 2
        problem_lines = (
            operand1.rjust(problem_width),
            f"{operator} {operand2.rjust(operand_len)}",
            "-"*problem_width,
            str(answer).rjust(problem_width),
        )
        arithmetic_problems.append(problem_lines)

    arranged_lines = [
        (" "*4).join(sections) for sections in zip(*arithmetic_problems)
    ]
    if not display_answers:
        arranged_lines.pop()

    arranged_problems = "\n".join(arranged_lines)

    return arranged_problems