Tell us what’s happening:
Describe your issue in detail here.
I think I have finished the arithmetic formatter and it looks like the code output is correct, but when it is put in the tester it only passes 2 tests. It looks like it follows all the rules for the challenge.
The error:
python main.py
32 3801 45 123
- 698 - 2 + 43 + 49
F.FFF.
FAIL: test_arrangement (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 14, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]’)
AssertionError: ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 -938[38 chars]----’ != ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 - 9[41 chars]----’
- 11 3801 1 123 1
- 11 3801 1 123 1
? +
-
- 4 - 2999 + 2 + 49 -9380
-
- 4 - 2999 + 2 + 49 - 9380
? +
- 4 - 2999 + 2 + 49 - 9380
- ---- ------ — ----- -----+ ---- ------ — ----- ------? +
: Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]
======================================================================
FAIL: test_only_digits (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 34, in test_only_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’)
python main.py
32 3801 45 123
- 698 - 2 + 43 + 49
F.FFF.
FAIL: test_arrangement (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 14, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]’)
AssertionError: ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 -938[38 chars]----’ != ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 - 9[41 chars]----’
- 11 3801 1 123 1
- 11 3801 1 123 1
? +
-
- 4 - 2999 + 2 + 49 -9380
-
- 4 - 2999 + 2 + 49 - 9380
? +
- 4 - 2999 + 2 + 49 - 9380
- ---- ------ — ----- -----+ ---- ------ — ----- ------? +
: Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]
======================================================================
FAIL: test_only_digits (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 34, in test_only_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’)
AssertionError: ’ 98 3801 45 123\n+ 3 - [49 chars]----’ != ‘Error: Numbers must only contain digits.’
- Error: Numbers must only contain digits.- 98 3801 45 123
-
- 3 - 2 + 43 + 49
- — ------ ---- ----- : Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”
======================================================================
FAIL: test_solutions (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/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 -38[79 chars] 172’ != ’ 32 1 45 123\n- 698 [87 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 ofTrue
.
======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 29, in test_too_many_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that has a number over 4 digits long to return “Error: Numbers cannot be more than four digits.”’)
AssertionError: ‘Error: Numbers cannot be more than four digits’ != ‘Error: Numbers cannot be more than four digits.’
- Error: Numbers cannot be more than four digits
- Error: Numbers cannot be more than four digits.
? +
: Expected calling “arithmetic_arranger()” with a problem that has a number over 4 digits long to return “Error: Numbers cannot be more than four digits.”
Ran 6 tests in 0.029s
FAILED (failures=4)
**Your code so far**
import re
def arithmetic_arranger(problems, show = False):
first_line = ""
second_line = ""
sum_dashes = ""
answer_line = ""
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
problem_split = re.findall(r"[\d]+", problem)
number = []
if "*" in problem or "/" in problem:
return "Error: Operator must be '+' or '-'."
for item in problem_split:
try:
number.append(int(item))
if len(item) > 4:
return "Error: Numbers cannot be more than four digits"
except:
return "Error: Numbers must only contain digits"
if "+" in problem:
operator = "+"
answer = number[0] + number[1]
if "-" in problem:
operator = "-"
answer = number[0] - number[1]
ans_long_num = [answer, number[0], number[1]]
longest_value_len = None
for value in ans_long_num:
if longest_value_len == None:
longest_value_len = len(str(value))
elif len(str(value)) > longest_value_len:
longest_value_len = len(str(value))
if longest_value_len == len(str(number[1])) or longest_value_len == len(str(number[0])):
longest_value_len = longest_value_len + 2
sum_dashes = sum_dashes + ("-" * longest_value_len + " ")
first_line = first_line + (str(number[0]).rjust(longest_value_len, " ") + " ")
second_line = second_line + operator + (str(number[1]).rjust(longest_value_len - 1, " ") + " ")
answer_line = answer_line + (str(answer).rjust(longest_value_len, " ") + " ")
if show == True:
arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + sum_dashes.rstrip() + "\n" + answer_line.rstrip()
if show == False:
arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + sum_dashes.rstrip()
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: