Tell us what’s happening:
I cannot figure out why when I run the code it only shows one formatted problem and no answer (Even when show_answers is True)
Your code so far
def arithmetic_arranger(problems, show_answers=True):
line1 = ''
line2 = ''
line_equal = ''
line_result = ''
if len(problems) > 5:
return(f'Error: Too many problems.')
for problem in problems:
parts = num1 , operator , num2 = problem.split()
if operator not in ['+','-']:
return(f'Error: Operator must be "+" or "-".')
if len(num1) > 4 or len(num2) > 4:
return(f'Error: Numbers cannot be more than four digits.')
if not num1.isdigit() or not num2.isdigit():
return(f'Error: Numbers must only contain digits.')
if show_answers:
if operator == '+':
result = str(int(num1) + int(num2))
elif operator == '-':
result = str(int(num1) - int(num2))
max_len = max(len(num1), len(num2)) + 2
line1 += num1.rjust(max_len) + ' '
line2 += operator + ' ' + num2.rjust(max_len - 2)
line_equal += '_' * max_len + ' '
if show_answers:
line_result += result.rjust(max_len) + ' '
formatted_problems = str(line1 + '\n' + line2 + '\n' + line_equal)
return formatted_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