I am presently finishing a course in python and I came across this glitch which has stumped me for a few days. The result should resemble this:
32 3801 45 123
+ 698 - 2 + 43 + 49
--- ---- -- ---
730 3799 88 172
However, it instead prints out this:
32 3801 45 123
+ 698 - 2 + 43 + 49
--- ---- -- --
730 3799 88 172
Please notice the sum of dashes in the fourth equation. In the correct version, the number of dashes correlates to the length of the max number. However, in the incorrect version (my version) the number of dashes is 2 when the length of the max number is 3.
Here is the entire code
equations = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_equation(arr):
stepOne = ''
stepTwo = ''
stepThree = ''
stepFour = ''
finalLine = ''
anotherLine = ''
thirdLine = ''
fourthLine = ''
space = 4
for item in equations:
parts = item.split()
x = parts[0]
y = parts[2]
c = parts[1]
if c == '+': z = (int(x) + int(y))
else: z = (int(x) - int(y))
n = len(x) if (x > y) else len(y)
stepOne = x.rjust(n + 2)
stepTwo = c + ' ' + y.rjust(n)
stepThree = ' ' + '-' * n
stepFour = str(z).rjust(n + 2)
finalLine += str(stepOne) + ' ' * space
anotherLine += str(stepTwo) + ' ' * space
thirdLine += str(stepThree) + ' ' * space
fourthLine += stepFour + ' ' * space
print(finalLine.rstrip() + '\n' + anotherLine + '\n' + thirdLine + '\n' + fourthLine)
arithmetic_equation(equations)