Tell us what’s happening:
Unable to understand why my code is unable to pass the tests. I have manually tried out the tests on my code and the output seems to be the same as the test answers. Could any kind soul enlighten me on what’s wrong?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
r1, r2, r3, r4 = '', '', '', ''
if len(problems) > 5:
print('Error: Too many problems.')
return None
for prob in problems:
num = prob.strip().split()
aoperand, operator, boperand = num[0], num[1], num[2]
if operator != '+' and operator != '-':
print("Error: Operator must be '+' or '-'.")
return None
if aoperand.isdigit() == False or boperand.isdigit() == False:
print('Error: Numbers must only contain digits.')
return None
if len(aoperand) > 4 or len(boperand) > 4:
print('Error: Numbers cannot be more than four digits.')
return None
if len(aoperand) > len(boperand):
width = len(aoperand) + 2
else:
width = len(boperand) + 2
if operator == '+' :
answer = str(int(aoperand) + int(boperand))
elif operator == '-' :
answer = str(int(aoperand) - int(boperand))
else :
pass
r1space = ' ' * (width - len(aoperand))
r2space = ' ' * (width - len(boperand) - len(operator))
r4space = ' ' * (width - len(answer))
r1 += r1space + aoperand + (' ' * 4)
r2 += operator + r2space + boperand + (' ' * 4)
r3 += (width * '-') + (' ' * 4)
r4 += r4space + answer + (' ' * 4)
if show_answers == True :
problems = f'{r1}\n{r2}\n{r3}\n{r4}'.rstrip()
else :
problems = f'{r1}\n{r2}\n{r3}'.rstrip()
return problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project