Hi everyone, I’ve been working on the arithmetic formatter project for the scientific computing with python projects class. I think I’ve created the right code but the code checker keeps throwing errors. I’m not sure what the issue is because the code checker is hard to parse.
Here is my code so far
import re
def arithmetic_arranger(problems, solve=False):
# Error check to see if there are too many problems
if len(problems) > 5:
return "Error: Too many problems."
# Initial declaration of string variables
num1 = ""
num2 = ""
op = ""
sumx = ""
top = ""
middle = ""
bottom = ""
total = ""
midline = ""
length = 0
# iterate through the inputs to obtain the numbers, operators, and lengths
for problem in problems:
# Regular expression check to see if the input has digits
if re.search(r'[^\s0-9.+-]', problem):
# Regular expression check to verify operator
if re.search(r'[/]', problem) or re.search(r'[*]', problem):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
# Split the input into the numbers and operator
problem = problem.split()
num1 = problem[0]
num2 = problem[2]
op = problem[1]
# Error check to see if the numbers are too long
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
# Obtaining the length of the longest number
length = max(len(num1), len(num2)) + 2
line = ""
for lines in range(length):
line += "-"
num1 = int(num1)
num2 = int(num2)
# Logical separators to compute addition or subtraction
if op == "+":
sumx = str(num1 + num2)
else:
sumx = str(num1 - num2)
# Assigning the top, middle and midline segments with proper spacing
top += str(num1).rjust(length) + " "
middle += (op + " " + str(num2)).rjust(length) + " "
midline += line.rjust(length) + " "
bottom += sumx.rjust(length) + " "
# Check to see if user wants to see the results
# Otherwise return the arranged problems
if solve:
total = top + "\n" + middle + "\n" + midline + "\n" + bottom + "\n"
else:
total = top + "\n" + middle + "\n" + midline + "\n"
return total
It seems like I’m following the project requirements such as having one space between the operator and the second digit and I have the four spaces separating between each problem. What is going wrong?
the error checker gives me:
======================== test session starts =========================
platform linux -- Python 3.10.11, pytest-6.2.5, py-1.11.0, pluggy-1.3.0 -- /nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/bin/python3
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] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [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' == ' 3801 123\n- 2 + 49\n------ -----'
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E - ------ -----
E + ------ -----
E ? +++++
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' == ' 1 1\n+ 2 - 9380\n--- ------'
E - 1 1
E + 1 1
E ? ++++
E - + 2 - 9380
E + + 2 - 9380
E ? ++++
E - --- ------
E + --- ------
E ? +++++
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') == (' 3 3801 45 123\n'\n '+ 855 - 2 + 43 + 49\n'\n '----- ------ ---- -----')
E - 3 3801 45 123
E + 3 3801 45 123
E ? ++++
E - + 855 - 2 + 43 + 49
E ? --- -
E + + 855 - 2 + 43 + 49
E ? +++ + ++++
E - ----- ------ ---- -----
E + ----- ------ ---- -----
E ? +++++
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') == (' 11 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '---- ------ --- ----- ------')
E - 11 3801 1 123 1
E + 11 3801 1 123 1
E ? ++++
E - + 4 - 2999 + 2 + 49 - 9380
E ? - -
E + + 4 - 2999 + 2 + 49 - 9380
E ? + + ++++
E - ---- ------ --- ----- ------
E + ---- ------ --- ----- ------
E ? +++++
test_module.py:77: AssertionError
__________ test_template[test_two_problems_with_solutions] ___________
arguments = [['3 + 855', '988 + 40'], True]
expected_output = ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'
@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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E assert (' 3 988 \n'\n '+ 855 + 40 \n'\n '----- ----- \n'\n ' 858 1028 \n') == ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
E - 3 988
E + 3 988
E ? ++++
E - + 855 + 40
E ? -
E + + 855 + 40
E ? + ++++
E - ----- -----
E + ----- -----
E ? ++++
E - 858 1028
E + 858 1028
E ? +++++
test_module.py:77: AssertionError
__________ test_template[test_five_problems_with_solutions] __________
arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = ' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'
@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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E assert (' 32 1 45 123 988 \n'\n '- 698 - 3801 + 43 + 49 + 40 \n'\n '----- ------ ---- ----- ----- \n'\n ' -666 -3800 88 172 1028 \n') == (' 32 1 45 123 988\n'\n '- 698 - 3801 + 43 + 49 + 40\n'\n '----- ------ ---- ----- -----\n'\n ' -666 -3800 88 172 1028')
E - 32 1 45 123 988
E + 32 1 45 123 988
E ? ++++
E - - 698 - 3801 + 43 + 49 + 40
E ? - -
E + - 698 - 3801 + 43 + 49 + 40
E ? + + ++++
E - ----- ------ ---- ----- -----
E + ----- ------ ---- ----- -----
E ? ++++
E - -666 -3800 88 172 1028
E + -666 -3800 88 172 1028
E ? +++++
test_module.py:77: AssertionError
====================== short test summary info =======================
FAILED test_module.py::test_template[test_two_problems_arrangement1]
FAILED test_module.py::test_template[test_two_problems_arrangement2]
FAILED test_module.py::test_template[test_four_problems_arrangement]
FAILED test_module.py::test_template[test_five_problems_arrangement]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
==================== 6 failed, 4 passed in 0.28s =====================```