Tell us what’s happening:
Why the answer didn’t come, there’s an error in my codes, can you help me please
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
a = ''
b = '\n'
c = '\n'
d = '\n'
for number in problems:
part = number.split()
#Abbreviation
n1 = part[0]
op = part[1]
n2 = part[2]
answer = 0
if op not in ["+", "-"]:
print('Error: Operator must be "+" or "-".')
if not n1.isdigit() or not n2.isdigit():
print('Error: Number must only contain digits.')
if len(n1) > 4 or len(n2) > 4:
print('Error: Numbers cannot be more than four digits')
if op == '+':
answer = int(n1) + int(n2)
elif op == '-':
answer = int(n1) - int(n2)
ans_len = len(str(answer))
dash_gap = (2 + max(len(n1), len(n2))) * '-'
n1_gap = (len(dash_gap) - len(n1)) * ' '
n2_gap = (len(dash_gap) - max(len(n1), len(n2)))
ans_gap = (len(dash_gap) - ans_len) * ' '
gap = 4 * ' '
a += f"{n1_gap}{n1}{gap}"
b += f"{op}{n2_gap}{n2}{gap}"
c += f"{dash_gap}{gap}"
d += f"{ans_gap}{answer}{gap}"
if show_answers:
result = a + b + c + d
else:
result = a + b + c
return result
arithmetic_arranger(["3801 - 2", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project