It gives me an error when I run the main module but my output matches that of expected output , it is properly arranged aswell.
def arithmetic_arranger(problems, answer=False):
Count = 0
for i in problems:
Count += 1
if Count > 5:
return "Error: Too many problems."
Loop = 0
for problem in problems:
Operands = problem.split(" ")
operator = Operands[1]
if operator not in ['+','-']:
return "Error: Operator must be \'+\' or \'-\'."
a = Operands[0]
b = Operands[2]
try:
x = int(a)
y = int(b)
except ValueError:
return "Error: Numbers must only contain digits."
n = len(a)
m = len(b)
if n > 4:
return "Error: Numbers cannot be more than four digits."
elif m > 4:
return "Error: Numbers cannot be more than four digits."
if n > m:
First_Line = ((n+2-n)*' ') + a
Second_Line = operator + ' ' + ((n-m)*' ') + b
Third_Line = ((n+2)*'-')
elif n < m:
First_Line = " " + ((m-n)*' ') + a
Second_Line = operator + ((m-n)*' ') + b
Third_Line = ((m+2)*'-')
elif n == m:
First_Line = " " + a
Second_Line = operator + " " + b
Third_Line = ((n+2)*'-')
if answer == True:
if operator == '+':
Fourth_Line = x + y
Fourth_Line = str(Fourth_Line)
space = len(Third_Line) - len(str(Fourth_Line))
Fourth_Line = (space* ' ') + Fourth_Line
elif operator == '-':
Fourth_Line = x - y
Fourth_Line = str(Fourth_Line)
space = len(Third_Line) - len(Fourth_Line)
Fourth_Line = (space* ' ') + Fourth_Line
if Loop == 0:
p = First_Line
q = Second_Line
r = Third_Line
s = Fourth_Line
elif Loop != 0:
p += " " + First_Line
q += " " + Second_Line
r += " " + Third_Line
s += " " + Fourth_Line
elif answer == False:
if Loop == 0:
p = First_Line
q = Second_Line
r = Third_Line
elif Loop != 0:
p += " " + First_Line
q += " " + Second_Line
r += " " + Third_Line
Loop += 1
if answer == True:
arranged = ( p + '\n' + q + '\n' + r + '\n' + s + '\n')
elif answer == False:
arranged = ( p + '\n' + q + '\n' + r + '\n')
return arranged
print(arithmetic_arranger(["32 + 679", "3800 - 2", "45 + 43", "123 + 4900", "770 - 9"], False))