Tell us what’s happening:
I am stuck with this challenge from the Python certification. I have tried to run the code in the Mu editor and works perfectly fine, so I don’t know what else to do…
Your code so far:
import re
def arithmetic_arranger(problems, solve = True):
if (len(problems) >5):
return "Error: Too many problems."
first = ""
second = ""
lines = ""
sumx = ""
string = ""
for problem in problems:
if(re.search("[^\s0-9.+-]", problem)):
if(re.search("[/]", problem) or re.search("[*]", problem)):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
firstNumber = problem.split(" ")[0]
operator = problem.split(" ")[1]
secondNumber = problem.split(" ")[2]
if len(firstNumber) >= 5 or len(secondNumber) >= 5:
return "Error: Numbers cannot be more than four digits"
sum = ""
if (operator == '+'):
sum = str(int(firstNumber) + int(secondNumber))
elif operator == '-':
sum = str(int(firstNumber) - int(secondNumber))
length = max(len(firstNumber), len(secondNumber)) + 2
top = str(firstNumber).rjust(length)
bottom = operator + str(secondNumber).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range(length):
line += "-"
if problem != problems[-1]:
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumx += res + ' '
else:
first += top
second += bottom
lines += line
sumx += res
if solve:
string = first + "\n" + second + "\n" + lines + "\n" + sumx
else:
string = first + "\n" + second + "\n" + lines
return string
Here is the fail message:
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
================================= test session starts ==================================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items
test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] FAILED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] PASSED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] PASSED [100%]
======================================= FAILURES =======================================
____________________ test_template[test_two_problems_arrangement1] _____________________
arguments = [['3801 - 2', '123 + 49']]
expected_output = ' 3801 123\n- 2 + 49\n------ -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 123\n- 2 + 49\n------ -----\n 3799 172' == ' 3801 123\n- 2 + 49\n------ -----'
E 3801 123
E - 2 + 49
E - ------ -----
E + ------ -----
E ? +
E + 3799 172
test_module.py:77: AssertionError
____________________ test_template[test_two_problems_arrangement2] _____________________
arguments = [['1 + 2', '1 - 9380']]
expected_output = ' 1 1\n+ 2 - 9380\n--- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E assert ' 1 1\n+ 2 - 9380\n--- ------\n 3 -9379' == ' 1 1\n+ 2 - 9380\n--- ------'
E 1 1
E + 2 - 9380
E - --- ------
E + --- ------
E ? +
E + 3 -9379
test_module.py:77: AssertionError
____________________ test_template[test_four_problems_arrangement] _____________________
arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = ' 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E assert (' 3 3801 45 123\n'\n '+ 855 - 2 + 43 + 49\n'\n '----- ------ ---- -----\n'\n ' 858 3799 88 172') == (' 3 3801 45 123\n'\n '+ 855 - 2 + 43 + 49\n'\n '----- ------ ---- -----')
E 3 3801 45 123
E + 855 - 2 + 43 + 49
E - ----- ------ ---- -----
E + ----- ------ ---- -----
E ? +
E + 858 3799 88 172
test_module.py:77: AssertionError
____________________ test_template[test_five_problems_arrangement] _____________________
arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = ' 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E assert (' 11 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '---- ------ --- ----- ------\n'\n ' 15 802 3 172 -9379') == (' 11 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '---- ------ --- ----- ------')
E 11 3801 1 123 1
E + 4 - 2999 + 2 + 49 - 9380
E - ---- ------ --- ----- ------
E + ---- ------ --- ----- ------
E ? +
E + 15 802 3 172 -9379
test_module.py:77: AssertionError
_________________________ test_template[test_too_many_digits] __________________________
arguments = [['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers cannot be more than four digits.'
fail_message = '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."'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: 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."
E assert 'Error: Numbers cannot be more than four digits' == 'Error: Numbers cannot be more than four digits.'
E - Error: Numbers cannot be more than four digits.
E ? -
E + Error: Numbers cannot be more than four digits
test_module.py:77: AssertionError
=============================== short test summary info ================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionError...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError...
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError...
FAILED test_module.py::test_template[test_too_many_digits] - AssertionError: Expected...
============================= 5 failed, 5 passed in 0.21s ==============================
Challenge: Arithmetic Formatter
Link to the challenge: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter
What can I do?
Thank you very much!!