Tell us what’s happening:
My output prints out correctly . but when I run the tests the test marks it wrong . what could the problem be ?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
# Check if there are too many problems (limit is 5)
if len(problems) > 5:
return "Error: Too many problems."
first = ""
second = ""
lines = ""
solt = ""
for problem in problems:
parts = problem.split()
# Split problem into three parts, separating operands from operator
first_problem, operator, second_problem = parts
if '+' not in operator and '-' not in operator:
return "Error: Operator must be '+' or '-'."
if not first_problem.isdigit() or not second_problem.isdigit():
return "Error: Numbers must only contain digits."
if len(first_problem) >= 5 or len(second_problem) >= 5:
return 'Error: Numbers cannot be more than four digits.'
length = max(len(first_problem), len(second_problem)) + 2
top = first_problem.rjust(length)
bottom = operator + second_problem.rjust(length - 1)
line = '-' * length
# Perform the operation based on the operator
if operator == '+':
res = str(int(first_problem) + int(second_problem)).rjust(length)
elif operator == '-':
res = str(int(first_problem) - int(second_problem)).rjust(length)
first += top + " "
second += bottom + " "
lines += line + " "
solt += res + " "
if show_answers:
arithmetic_arranger = first + '\n' + second + '\n' + lines + '\n' + solt
else:
arithmetic_arranger = first + '\n' + second + '\n' + lines
return arithmetic_arranger
# Example usage:
print(f'\n{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/118.0.0.0 Safari/537.36 Edg/118.0.2088.46
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project