Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

even when my code displays the correct result for all the cases it still gives me an error

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        raise ValueError("Error: Too many problems.")

    top_row = []
    bottom_row = []
    dash_row = []
    result_row = []

    for problem in problems:
        num1, operator, num2 = problem.split()

        if operator not in ("+", "-"):
            raise ValueError("Error: Operator must be '+' or '-'.")

        if not num1.isdigit() or not num2.isdigit():
            raise ValueError("Error: Numbers must only contain digits.")

        if len(num1) > 4 or len(num2) > 4:
            raise ValueError("Error: Numbers cannot be more than four digits.")

        result = int(num1) + int(num2) if operator == "+" else int(num1) - int(num2)

        width = max(len(num1), len(num2)) + 2

        top_row.append(num1.rjust(width))
        bottom_row.append(f'{operator} {num2.rjust(width - 2)}')  
        dash_row.append("-" * width)  
        result_row.append(str(result).rjust(width))

    print("    ".join(top_row))
    print("    ".join(bottom_row))
    print("    ".join(dash_row))
    
    if show_answers:
        print("    ".join(result_row))
    
        
arithmetic_arranger(["3801 - 2", "123 + 49"],True)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

What your function returns?

Please share more information.

What is the error?
What does your function return?

returns the problems

Your function needs to return the answer, not print it. Test your functions return like this:

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

even this doesn’t work:

def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
raise ValueError(“Error: Too many problems.”)

top_row = []
bottom_row = []
dash_row = []
result_row = []

for problem in problems:
    num1, operator, num2 = problem.split()

    if operator not in ("+", "-"):
        raise ValueError("Error: Operator must be '+' or '-'.")

    if not num1.isdigit() or not num2.isdigit():
        raise ValueError("Error: Numbers must only contain digits.")

    if len(num1) > 4 or len(num2) > 4:
        raise ValueError("Error: Numbers cannot be more than four digits.")

    result = int(num1) + int(num2) if operator == "+" else int(num1) - int(num2)

    width = max(len(num1), len(num2)) + 2

    top_row.append(num1.rjust(width))
    bottom_row.append(f'{operator} {num2.rjust(width - 2)}')  
    dash_row.append("-" * width)  
    result_row.append(str(result).rjust(width))

final_result = "\n".join([
    "    ".join(top_row),
    "    ".join(bottom_row),
    "    ".join(dash_row)
])

if show_answers:
    final_result += "\n" + "    ".join(result_row)

return final_result

when I take the value given by test cases and test them in the console the output is correctly returned, but still, the test failed for some reason

i have figured it out, i should replace the “raise ValueError” statement with return too

1 Like