[’ 32\n+ 698\n_____\n 730’, ’ 3801\n- 2\n______\n 3799’, ’ 45\n+ 43\n____\n 88’, ’ 123\n+ 49\n_____\n 172’]
I am getting results like this, how can i fix that?
def arithmetic_arranger(problems, BOOL = True):
if len(problems) > 5:
return "error1"
else:
arranged = []
for problem in problems:
x = problem.split()
num1 = x[0]
num2 = x[2]
oper = x[1]
if oper != "+" and oper != "-":
return "error2+-"
if not num1.isdigit() or not num2.isdigit():
return "number contains digits"
else:
if len(num1) > 4 or len(num2) > 4:
return "too long"
else:
if oper == "+":
answer = int(num1) + int(num2)
else:
answer = int(num1) - int(num2)
maxim = max(len(num1), len(num2))
width = maxim + 2
first_line = str(num1.rjust(width))
second_line = oper + str(num2.rjust(width - 1))
third = "_" * width
forth = str(answer).rjust(width)
if BOOL:
arranged.append(first_line + "\n" + second_line + "\n" + third + "\n" + forth)
else:
arranged.append(first_line + "\n" + second_line + "\n" + third)
return arranged