Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code complies with all the instructions and displays exact output yet I don’t understand why the Test run fails. I’m sharing a screenshot of the verbose output.
Kindly review my code and help!

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    operand_one_list = ''
    operand_two_list = ''
    base_line = ''
    answer_list = ''
    problems_length = len(problems)
    if problems_length > 5 :
        raise ValueError('Error: Too many problems.')
    problems_clubbed = ''.join(problems)
    for char in ' +-':
        problems_clubbed = problems_clubbed.replace(char, '')
    if problems_clubbed.isnumeric():
        for problem in problems:
            operand_one = problem[0:problem.index(' ')]
            operator_sign = problem[problem.index(' ') + 1:problem.index(' ') + 2]
            operand_two = problem[problem.index(' ')+3:]
            sum_of_operands = ''
            if len(operand_one) > 4 or len(operand_two) > 4:
                raise ValueError('Error: Numbers cannot be more than four digits.') 
            else:
                largest_operand = max(len(operand_one), len(operand_two))
                for _ in range(largest_operand - len(operand_one)):
                    operand_one_list += ' '
                operand_one_list += f'  {operand_one}    '
                operand_two_list += f'{operator_sign} '
                for _ in range(largest_operand - len(operand_two)):
                    operand_two_list += ' '
                operand_two_list += f'{operand_two}    '
                for _ in range(largest_operand + 2):
                    base_line += '-'
                base_line += '    '
                if operator_sign == '+':
                    sum_of_operands = str(int(operand_one) + int(operand_two))
                elif operator_sign == '-':
                    sum_of_operands = str(int(operand_one) - int(operand_two))
                else:
                    raise ValueError("Error: Operator must be '+' or '-'.")
                for _ in range((largest_operand + 2) - len(sum_of_operands)):
                    answer_list += ' '
                answer_list += f'{sum_of_operands}    '
    else:
        if (problems_clubbed.find('/') > -1) or (problems_clubbed.find('*') > -1):
            raise ValueError("Error: Operator must be '+' or '-'.")
        else:
            raise TypeError('Error: Numbers must only contain digits.')
    if show_answers is True:
        return (f'{operand_one_list}\n{operand_two_list}\n{base_line}\n{answer_list}\n')
    else:
        return (f'{operand_one_list}\n{operand_two_list}\n{base_line}\n')

test1 = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
print(f'\nTest 1\n======\n{test1}')

test2 = arithmetic_arranger(["3 + 855", "988 + 40"], True)
print(f'\nTest 2\n======\n{test2}')

# test3 = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 3\n======\n{test3}')

# test4 = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 4\n======\n{test4}')

# test5 = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
# print(f'\nTest 5\n======\n{test5}')

# test6 = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
# print(f'\nTest 6\n======\n{test6}')

test7 = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
print(f'\nTest 7\n======\n{test7}')

test8 = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
print(f'\nTest 8\n======\n{test8}')

test9 = arithmetic_arranger(["1 + 2", "1 - 9380"])
print(f'\nTest 9\n======\n{test9}')

test10 = arithmetic_arranger(["3801 - 2", "123 + 49"])
print(f'\nTest 10\n=======\n{test10}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

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         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

Troubleshoot one thing at a time.

Start with the first test, and use repr() to format your output as the test does:

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
>>>'  3801      123    \n-    2    +  49    \n------    -----    \n'
arithmetic_arranger(["3801 - 2", "123 + 49"])` should return `  3801      123\n-    2    +  49\n------    -----.

Now it’s easy to compare your output with the expected output:

'  3801      123    \n-    2    +  49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.