Tell us what’s happening:
I am working on the assigment for arithmetic formatter.
The code seems to work and the solution seems correct, yet the tests returns an error. Could someone tell me what the difference between my answer is and whats expected?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
errorMessage = ''
line1 = ''
line2 = ''
line3 = ''
answers = ''
if len(problems) > 5:
errorMessage = 'Error: Too many problems.'
for problem in problems:
split = problem.split(' ')
operand1 = split[0]
operator = split[1]
operand2 = split[2]
spaceAfter = (4 * ' ')
if operator == '*' or operator == '/' in problem:
errorMessage = "Error: Operator must be '+' or '-'."
if not operand1.isdigit() or not operand2.isdigit():
errorMessage = 'Error: Numbers must only contain digits.'
if len(operand1) > 4 or len(operand2) > 4:
errorMessage = 'Error: Numbers cannot be more than four digits.'
maxlength = max(len(operand1), len(operand2))
absolute = abs(len(operand1) - len(operand2))
if len(operand1) > len(operand2):
operand2 = (absolute * ' ') + operand2
else:
operand1 = (absolute * ' ') + operand1
line1 += (' {}{}'.format(operand1,spaceAfter))
line2 += ('{} {}{}'.format(operator,operand2,spaceAfter))
line3 += ('{}{}'.format(((maxlength + 2) * '-'),spaceAfter))
answers += (' {}{}').format(eval(problem),spaceAfter)
line1 += '\n'
line2 += '\n'
problems = line1 + line2 + line3
if show_answers:
problems += '\n'+ answers
if errorMessage:
return errorMessage
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
