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