Tell us what’s happening:
I believe I have the desired output and code, but the output is said to be incorrect. Need Help plz.
Your code so far
def format(problems):
if len(problems) > 5:
return 'Error: Too many problems.'
for problem in problems:
parts = problem.split()
if parts[1] not in ('+', '-'):
return "Error: Operator must be '+' or '-'."
if not (parts[0].isdigit() and parts[2].isdigit()):
return 'Error: Numbers must only contain digits.'
if len(parts[0]) > 4 or len(parts[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
return None
def conversion(problems, show_answers):
top_line = []
bottom_line = []
dashed_line = []
answer_line = []
for problem in problems:
parts = problem.split()
left = parts[0]
operator = parts[1]
right = parts[2]
width = max(len(left), len(right)) + 2
top = left.rjust(width)
bottom = operator + right.rjust(width - 1)
dashes = '-' * width
top_line.append(top)
bottom_line.append(bottom)
dashed_line.append(dashes)
if show_answers:
if operator == '+':
answer = str(int(left) + int(right))
else:
answer = str(int(left) - int(right))
answer_line.append(answer.rjust(width))
top_result = ' '.join(top_line)
bottom_result = ' '.join(bottom_line)
dashed_result = ' '.join(dashed_line)
conversions = top_result + '\n' + bottom_result + '\n' + dashed_result
if show_answers:
answer_result = ' '.join(answer_line)
conversions += '\n' + answer_result
return conversions
def arithmetic_arranger(problems, show_answers=True):
error = format(problems)
if error:
return error
return conversion(problems, show_answers)
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
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/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
