For the life of me, I can't figure out why my Python Arithmetic Formatter fails

Maybe it’s not the prettiest code, but the output seems to match what is expected/required. But it fails ALL the checks. I can’t figure out why. Any ideas?

def arithmetic_arranger(problems, solutions=False):
    equation_len = len(problems)
    if equation_len > 5:
        return print("Error: Too many problems.")
    inpt_split = []
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""
    for calc in problems:
        split_calc = calc.split()
        inpt_split.append(split_calc)
    for num1, term, num2 in inpt_split:
        if term != "+" and term != "-":
            return print("Error: Operator must be '+' or '-'.")
        if len(num1) > 4 or len(num2) > 4:
            return print("Error: Numbers cannot be more than four digits.")
        try:
            int(num1)
            int(num2)
        except ValueError:
            return print("Error: Numbers must only contain digits.")
        max_length = max(len(num1)+2, len(num2)+2)
        line1 += " "*(max_length-len(num1)) + num1 + "    "
        line2 += term + " "*(max_length-len(num2)-1) + num2 + "    "
        line3 += "-"*max_length + "    "
        if term == "+":
            sums = int(num1) + int(num2)
        elif term == "-":
            sums = int(num1) - int(num2)
        line4 += " "*(max_length-len(str(sums))) + str(sums) + "    "
    if solutions == False:
        line4 = ""
    arranged_problems = print(f"{line1.rstrip()}\n{line2.rstrip()}\n{line3.rstrip()}\n{line4.rstrip()}")
    return arranged_problems

The problem asked your function to return a string with the appropriate content…what you seem to be doing in your program’s return statement is not returning a string, but instead printing the string to screen, which in turn would return a value of None.

return "Hello World" is not the same as
return print("Hello World")

Thank you! All I had to do was remove the print() from each return line… and it completely passed!

For whatever reason I wasn’t getting “none” on my output in Replit… weird. But that worked. Appreciate it is much.

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