Tell us what’s happening:
I know my problem is that I am printing the final answer and not returning it. I just cannot seem to figure out the difference between print and return. Please help!
Your code so far
def arithmetic_arranger(problems, show_answers=False):
line_1 = ''
line_2 = ''
line_3 = ''
line_4 = ''
#Error Codes
if len(problems)>5:
return 'Error: Too many problems.'
for problem in problems:
num1, operator, num2 = problem.split()
if (operator == '*') or (operator == '/') :
return "Error: Operator must be '+' or '-'."
if (len(num1)>4) or (len(num2)>4):
return 'Error: Numbers cannot be more than four digits.'
for char in problem:
if char.isalpha():
return 'Error: Numbers must only contain digits.'
num1_len = len(num1)
num2_len = len(num2)
long_num = max(num1_len, num2_len)
num1_space = (long_num + 2) - num1_len
num2_space = (long_num +1) - num2_len
problem_spacing = ' '*4
if operator == '+':
answer = int(num1) + int(num2)
elif operator == '-':
answer = int(num1) - int(num2)
answer_len = len(str(answer))
answer_space = (long_num + 2) - answer_len
line_1 += ' '*num1_space + num1 + ' '*4
line1 = line_1
line_2 += operator + ' '*num2_space + num2 + ' '*4
line2 = line_2
line_3 += '-'*(long_num + 2) + ' '*4
line3 = line_3
line_4 += ' '*answer_space + str(answer) + ' '*4
line4 = line_4
if show_answers == True:
final_answer = f'{line_1}\n{line_2}\n{line_3}'
else:
final_answer = f'{line_1}\n{line_2}\n{line_3}\n{line_4}'
print(final_answer)
arithmetic_arranger(["3801 - 2", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project