Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I’ve been puzzling over this for a few days now, it passes the error checks and if i put in the required test by hand the output looks identical to the should return segments but it doesnt pass them in the automated tests?

Your code so far

import re

def arithmetic_arranger(problems, show_answers=False):
#error handling

if len(problems) > 5:
    return 'Error: Too many problems.'

numbers = []  
for i in problems:
    p = i.split()
    numbers.extend([p[0], p[2]])

if not all(map(lambda x: x.isdigit(), numbers)):
    return "Error: Numbers must only contain digits."

for problem in problems:
    if (re.search("['/']", problem) or re.search("['*']", problem)):
        return "Error: Operator must be '+' or '-'."

#formatting

first_number = list(map(lambda x: x.split()[0], problems))
operator = list(map(lambda x: x.split()[1], problems))
second_number = list(map(lambda x: x.split()[2], problems))
result = list(map(lambda x: str(eval(x)), problems))

first_number_line = ''
operator_second_number_line = ''
dashes_line = ''
results_line = ''
gap = '    '


for i in range(len(problems)):
    #last error check
    if len(first_number[i]) > 4 or len(second_number[i]) > 4:
        return 'Error: Numbers cannot be more than four digits.'
    
    #formatting
    width = max(len(first_number[i]), len(second_number[i])) + 2
    first_number_line += first_number[i].rjust(width) + gap
    operator_second_number_line += operator[i] + second_number[i].rjust(width - 1) + gap
    dashes_line += '-' * width + gap
    results_line += result[i].rjust(width) + gap

#show answers flag
if show_answers:
    formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line, results_line))
else:
    formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line))


return formatted_problems
import re

def arithmetic_arranger(problems, show_answers=False):
    #error handling
 
    if len(problems) > 5:
        return 'Error: Too many problems.'
    
    numbers = []  
    for i in problems:
        p = i.split()
        numbers.extend([p[0], p[2]])
    
    if not all(map(lambda x: x.isdigit(), numbers)):
        return "Error: Numbers must only contain digits."

    for problem in problems:
        if (re.search("['/']", problem) or re.search("['*']", problem)):
            return "Error: Operator must be '+' or '-'."

    #formatting

    first_number = list(map(lambda x: x.split()[0], problems))
    operator = list(map(lambda x: x.split()[1], problems))
    second_number = list(map(lambda x: x.split()[2], problems))
    result = list(map(lambda x: str(eval(x)), problems))

    first_number_line = ''
    operator_second_number_line = ''
    dashes_line = ''
    results_line = ''
    gap = '    '


    for i in range(len(problems)):
        #last error check
        if len(first_number[i]) > 4 or len(second_number[i]) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        
        #formatting
        width = max(len(first_number[i]), len(second_number[i])) + 2
        first_number_line += first_number[i].rjust(width) + gap
        operator_second_number_line += operator[i] + second_number[i].rjust(width - 1) + gap
        dashes_line += '-' * width + gap
        results_line += result[i].rjust(width) + gap

    #show answers flag
    if show_answers:
        formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line, results_line))
    else:
        formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line))
    

    return formatted_problems


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

1 Like

You are almost there! Open the browser console to see a more detailed output from the tests

The assertion error and diff 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

This is called a diff, and it shows you the differences between two files or blocks of code:

- 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!

2 Likes

thank you so much just got it and it passes everything, got clearer insight using the console log on chrome and dropping that into notepad++ but your direction got me thinking right!