Tell us what’s happening:
Hello I keep failing the first 4 parts of the arithmetic formatter project and can’t seem to understand why, I am passing all other parts of the test
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
first_line = ''
second_line = ''
dashes_line = ''
answer_line = ''
for i, problem in enumerate(problems):
parts = problem.split()
operand1 = parts[0]
operator = parts[1]
operand2 = parts[2]
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if len(operand1) > 4 or len(operand2) >4:
return 'Error: Numbers cannot be more than four digits.'
if not (operand1.isdigit() and operand2.isdigit()):
return 'Error: Numbers must only contain digits.'
width = max(len(operand1), len(operand2)) + 2
first_line += operand1.rjust(width)
second_line += operator + ' ' + operand2.rjust(width - 2)
dashes_line += '-' * width
if show_answers:
if operator == '+':
answer = str(int(operand1) + int(operand2))
else:
answer = str(int(operand1) - int(operand2))
answer_line += answer.rjust(width)
if i < len(problems) - 1:
first_line += ' '
second_line += ' '
dashes_line += ' '
if show_answers:
answer_line += ' '
if show_answers:
final_problems = first_line + '\n' + second_line + '\n' + dashes_line + '\n' + answer_line
else: final_problems = first_line + '\n' + second_line + '\n' + dashes_line + '\n'
return final_problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project