Tell us what’s happening:
Describe your issue in detail here.
I don’t really understand my error messages anymore and what could I do to solve the project.
python main.py
32 3801 45 123
- 698 - 2 + 43 + 49
F…F…
FAIL: test_arrangement (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/test_module.py”, line 10, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]’)
AssertionError: ’ 3 3801 45 123\n+ 855 - [53 chars]----’ != ’ 3 3801 45 123\n+ 855 - 2 [44 chars]----’
-
3 3801 45 123
? - - -
-
3 3801 45 123
-
- 855 - 2 + 43 + 49
? - - -
- 855 - 2 + 43 + 49
-
- 855 - 2 + 43 + 49
- ----- ------ ---- -----? - - -
- ----- ------ ---- ----- : Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]
======================================================================
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: ’ 32 1 45 123\n- 698 - 3801 [90 chars] 172’ != ’ 32 1 45 123\n- 698 - 3801 + [75 chars] 172’
- 32 1 45 123
? - - -
- 32 1 45 123
-
- 698 - 3801 + 43 + 49
? - - -
- 698 - 3801 + 43 + 49
-
- 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.005s
FAILED (failures=2)
Your code so far
import re
def arithmetic_arranger(problems, solve=False):
#the reason that the solve parameter is set to false is that the person making the operation need to set it to true for to be performed, if not it will pass
#promblem list
firstvar = ''
secondvar = ''
sumvars = ''
solutlines = ''
#too many problems
if(len(problems) > 5):
return 'Error: Too many problems.'
#spliting the problems
for problem in problems:
if re.search(’[^\s0-9.±]’, problem):
if re.search(’[/]’, problem) or re.search(’[*]’, problem):
#this will look for any division or multiplication in the code and if it happens…
return “Error: Operator must be ‘+’ or ‘-’.”
#this will match any white space in the problem and if it happens…
return “Error: Numbers must only contain digits.”
first = problem.split(" ")[0]
operator = problem.split(" ")[1]
second = problem.split(" ")[2]
#this will find the variables to be used in the operations
if len(first) >= 5 or len(second) >= 5:
return "Error: Numbers cannot be more than four digits."
#construction of the operations
sum = ""
if (operator == "+"):
sum = str(int(first) + int(second))
elif (operator == "-"):
sum = str(int(first) - int(second))
#setingup & adjusting the layout of the operation
length = max(len(first), len(second)) + 2
top = str(first).rjust(length)
bottom = operator + str(second).rjust(length - 1)
line = ""
resp = str(sum).rjust(length)
for s in range(length):
line += "-"
if problem != problems[-1]:
firstvar += top + ' '
secondvar += bottom + ' '
solutlines += line + ' '
sumvars += resp + ' '
else:
firstvar += top
secondvar += bottom
solutlines += line
sumvars += resp
if solve:
arranged_problems = firstvar + "\n" + secondvar + "\n" + solutlines + "\n" + sumvars
else:
arranged_problems = firstvar + "\n" + secondvar + "\n" + solutlines
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: