Help with rearranging strings to pass unit tests in arithmetic formatting

I’ve gotten through the first challenge but the unit tests are failing even though the strings output look exactly like the readme file included.

As a side note, the examples for the replit look different than the ones with the readme if you download the files and do it in VS code like I have.

Here is the code for the arithmetic arranger function
def arithmetic_arranger(problems, showAnswers = False):

listOfProblems = list(problems)

#check to see if there are too many problems

if len(listOfProblems) > 5:

    return "Error: Too many problems."



for problem in listOfProblems:

    #check for the existence of multiplication or division

    x = re.findall("[a-zA-Z]",problem)

    if (len(x) > 0):

        return "Error: Numbers must only contain digits."

    partsList = problem.split()    

    if partsList[1] == '/' or partsList[1] == '*' :

        return "Error: Operator must be '+' or '-'."

    z = len(partsList[0])

    y = len(partsList[2])

    if ( z > 4 or y > 4 ):

        return "Error: Numbers cannot be more than four digits."

   

#if I have made it this far input is good

#create lists for building up the output string

listOfFirstNumbers = list()

listOfLastNumbers = list()

listOfAdditionOrSubtractionCharacters = list()

listOfLongestLength = list()

#populate the lists

for problem in listOfProblems:

    partsList = problem.split()

    listOfFirstNumbers.append(int(partsList[0]))

    listOfLastNumbers.append(int( partsList[2]))

    listOfAdditionOrSubtractionCharacters.append(partsList[1])

    length = 0

    for part in partsList:

        if (len(part) > length):

            length = (len(part))

    listOfLongestLength.append(length + 2)

         

#create the string

string_builder_first_line = ""

string_builder_second_line = ""

string_builder_third_line = ""

string_builder_fourth_line=""

for i in range(len(listOfFirstNumbers)):

   

    string_builder_first_line +=  str(listOfFirstNumbers[i]).rjust(listOfLongestLength[i]) + '    '

    string_builder_second_line += (str(listOfAdditionOrSubtractionCharacters[i]) + ' ' + str(listOfLastNumbers[i]).rjust(listOfLongestLength[i] - 2)) + '    '

    for j in range(listOfLongestLength[i]):

        string_builder_third_line += '-'

    string_builder_third_line += '    '

    if ( i == (len(listOfFirstNumbers)-1)):

        string_builder_first_line.rstrip()

        string_builder_second_line.rstrip()

        string_builder_third_line.rstrip()

        string_builder_second_line+= '\n'

        string_builder_first_line+='\n'

        string_builder_third_line+='\n'

#print(string_builder_first_line)

#print(string_builder_second_line)

#print(string_builder_third_line)



answer = 0

for i in range(len(listOfAdditionOrSubtractionCharacters)):

    if(listOfAdditionOrSubtractionCharacters[i] == '-'):

        answer = listOfFirstNumbers[i] - listOfLastNumbers[i]

    else:

        answer = listOfFirstNumbers[i] + listOfLastNumbers[i]

    string_builder_fourth_line += str(answer).rjust(listOfLongestLength[i]) + '    '

    if ( i == (len(listOfFirstNumbers)-1)):

        string_builder_fourth_line.rstrip()



if (showAnswers == True):

    return string_builder_first_line + string_builder_second_line + string_builder_third_line + string_builder_fourth_line

else:

    return string_builder_first_line + string_builder_second_line + string_builder_third_line

If anyone can tell me why it is failing I’d appreciate it. When I run it in the VS code terminal it looks right.

Can you provide the error messages (always a best practice when asking for help)? Usually this is caused by spacing problems.

========================================================================== 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 … ----- \n’ == ’ 3801 …---- -----’
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E ? ++++
E - ------ -----…
E
E …Full output truncated (3 lines hidden), use ‘-vv’ to show

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 … ------ \n’ == ’ 1 …— ------’
E - 1 1
E + 1 1
E ? ++++
E - + 2 - 9380
E + + 2 - 9380
E ? ++++
E - — ------…
E
E …Full output truncated (3 lines hidden), use ‘-vv’ to show

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 3… ----- \n’ == ’ 3 3…---- -----’
E - 3 3801 45 123
E + 3 3801 45 123
E ? ++++
E - + 855 - 2 + 43 + 49
E + + 855 - 2 + 43 + 49
E ? ++++
E - ----- ------ ---- -----…
E
E …Full output truncated (3 lines hidden), use ‘-vv’ to show

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 38… ------ \n’ == ’ 11 38…— ------’
E - 11 3801 1 123 1
E + 11 3801 1 123 1
E ? ++++
E - + 4 - 2999 + 2 + 49 - 9380
E + + 4 - 2999 + 2 + 49 - 9380
E ? ++++
E - ---- ------ — ----- ------…
E
E …Full output truncated (3 lines hidden), use ‘-vv’ to show

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 9… 1028 ’ == ’ 3 9… 858 1028’
E - 3 988
E + 3 988
E ? ++++
E - + 855 + 40
E + + 855 + 40
E ? ++++
E - ----- -----…
E
E …Full output truncated (6 lines hidden), use ‘-vv’ to show

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.’

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 - 32 1 45 123 988
E + 32 1 45 123 988
E ? ++++
E - - 698 - 3801 + 43 + 49 + 40
E + - 698 - 3801 + 43 + 49 + 40
E - ----- ------ ---- ----- -----…
E
E …Full output truncated (6 lines hidden), use ‘-vv’ to show

exampleOutput

That looks like you have extra spaces at the end of each line.

1 Like