Hi, I am still a beginner so I tried to create the code in the most comprehensive way I could, but I still can not understand what is the problem with the spaces in the result. I would really appreciate it if someone could help me.
My code:
def arithmetic_arranger(problems, status = False):
#initialize variables
numbers1 = []
oper = []
numbers2 = []
if len(problems) > 5:
return "Error: Too many problems."
#loops trough values and Checks for errors
for i in problems :
problem = i.split()
numbers1.append(problem[0])
if problem[1] != "+" and problem[1] != "-":
return "Error: Operator must be '+' or '-'."
oper.append(problem[1])
numbers2.append(problem[2])
#Checks if the input are only numbers
if not (problem[0].isdigit() and problem[2].isdigit()):
return "Error: Numbers must only contain digits."
#Checks lenght of given numbers
if len(problem[0]) > 4 or len(problem[2]) > 4:
return "Error: Numbers cannot be more than four digits."
arranged_problems = ""
#initialize rows
topRow = ""
bottomRow = ""
separator = ""
resultsFormat = ""
#Figures out the amount of spaces in top and bottom row by the lenght of the numbers
for i in range(len(problems)):
if len(numbers1[i]) > len(numbers2[i]):
totalLen = len(numbers1[i]) + 2
else:
totalLen = len(numbers2[i]) + 2
if i != 0 :
topRow += " "
bottomRow += " "
separator += " "
#writes top row
topRow += " " * (totalLen - len(numbers1[i]))
topRow += numbers1[i]
#writes bottom row
bottomRow += oper[i] + ( " " * (totalLen - len(numbers2[i]) - 1))
bottomRow += numbers2[i]
#writes separator
separator += "-" * totalLen
arranged_problems = "{}\n{}\n{}" .format(topRow, bottomRow, separator)
#Calculates the results
if status == True :
for i in range(len(problems)):
if oper[i] == "+":
result = int(numbers1[i]) + int(numbers2[i])
elif oper[i] == "-":
result = int(numbers1[i]) - int(numbers2[i])
if i != 0 :
resultsFormat += " "
resultsFormat += " " * (totalLen - len(str(result)))
resultsFormat += str(result)
arranged_problems += "\n{}" .format(resultsFormat)
return arranged_problems
Error message:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
...F..
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: ' 3[68 chars]- ------ ---- -----\n -666 -3800 88 172' != ' 3[68 chars]- ------ ---- -----\n -666 -3800 88 172'
32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
- -666 -3800 88 172? -
+ -666 -3800 88 172? +
: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.006s
FAILED (failures=1)