Tell us what’s happening:
Hello! Could you please help me?
The details of the problem:
When I run the tests, the tests whose outputs are supposed to be Error messages pass the test. But, the remaining tests, where correct values are inputted, they fail the test. I have spent a lot of time looking for errors in my code, and as well as manually checking the expected test output, and my test output; and run the tests again, but they don’t pass the test. Hence, could you please check ad let me know what is wrong with it?
My code so far:
def arithmetic_arranger(problems, show_answers=False):
if len(problems)>5:
return 'Error: Too many problems.'
operands1=''
line2=''
lines=''
answers=''
for problem in problems:
operand1=problem.split(' ')[0]
operator = problem.split(' ')[1]
operand2= problem.split(' ')[2]
if operator != '+' and operator != '-':
return "Error: Operator must be '+' or '-'."
if not(operand1.isdigit() and operand2.isdigit()):
return 'Error: Numbers must only contain digits.'
if len(operand1)>4 or len(operand2)>4:
return 'Error: Numbers cannot be more than four digits.'
line=''
if operator=='+':
answer = str(int(operand1) + int(operand2))
elif operator=='-':
answer = str(int(operand1) - int(operand2))
width = max(len(operand1), len(operand2))+2
if problem!=problems[-1]:
operands1 += str(operand1).rjust(width)+' '
line2 += operator+str(operand2).rjust(width-1)+' '
lines += '-'*width +' '
answers += str(answer).rjust(width)+' '
else:
operands1 += str(operand1).rjust(width)
line2 += operator+str(operand2).rjust(width-1)
lines += '-'*width
answers += str(answer).rjust(width)
if show_answers==True:
arranged=operands1 + '\n' + line2 + '\n' + lines + '\n' + answers
elif show_answers==False:
arranged=operands1 + '\n' + line2 + '\n' + lines
return arranged
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project