Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code works as it’s meant to but it only passes tests 5 , 7 and 8.
What am I meant to do to earn my certification? Has anybody actually gotten it?

Your code so far

def arithmetic_arranger(problems, show_answers=True):
    line1 = '    '
    line2 = '    '
    line_equal = '    '
    line_result = '    '
    
    # Check if there are too many problems
    if len(problems) > 5:
        return 'Error: Too many problems.'
    
    # Process each problem
    for problem in problems:
        parts = problem.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        
        # Validate the operator
        if operator not in ['+', '-']:
            return 'Error: Operator must be "+" or "-".'
        
        # Validate the length of the numbers
        if len(num1) > 4 or len(num2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        
        # Validate that the numbers contain only digits
        if not num1.isdigit() or not num2.isdigit():
            return 'Error: Numbers must only contain digits.'
        
        # Calculate the result if required
        if show_answers:
            if operator == '+':
                result = str(int(num1) + int(num2))
            elif operator == '-':
                result = str(int(num1) - int(num2))
        
        # Determine the maximum length for formatting
        max_len = max(len(num1), len(num2)) + 2
        
        # Format the lines
        line1 += num1.rjust(max_len) + '    '  # 4 spaces between problems
        line2 += operator + ' ' + num2.rjust(max_len - 2) + '    '
        line_equal += '-' * max_len + '    '
        
        # Add the result line if required
        if show_answers:
            line_result += result.rjust(max_len) + '    '
    
    # Combine the lines into the final formatted string
    if show_answers:
        formatted_problems = str(line1.rjust(max_len) + '\n' + line2.rjust(max_len) + '\n' + line_equal + '\n' + line_result.rjust(max_len))
    else: #if no result is required
        formatted_problems = str(line1.rjust(max_len) + '\n' + line2.rjust(max_len) + '\n' + line_equal)
    
    return formatted_problems


print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum @dinosaursarealive

There is an issue with the spacing.

i

Compare to the expected format.

Happy coding

1 Like

When I compare it to the given formatting now, it looks the same but still doesn’t pass the tests.

def arithmetic_arranger(problems, show_answers = False):
    line1 = ''
    line2 = ''
    line_equal = ''
    line_result = ''
    
    # Check if there are too many problems
    if len(problems) > 5:
        return 'Error: Too many problems.'
    
    # Process each problem
    for problem in problems:
        parts = problem.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        
        # Validate the operator
        if operator not in ['+', '-']:
            return 'Error: Operator must be "+" or "-".'
        
        # Validate the length of the numbers
        if len(num1) > 4 or len(num2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        
        # Validate that the numbers contain only digits
        if not num1.isdigit() or not num2.isdigit():
            return 'Error: Numbers must only contain digits.'
        
        # Calculate the result if required
        if show_answers:
            if operator == '+':
                result = str(int(num1) + int(num2))
            elif operator == '-':
                result = str(int(num1) - int(num2))
        
        # Determine the maximum length for formatting
        max_len = max(len(num1), len(num2)) + 2
        
        # Format the lines
        line1 += num1.rjust(max_len) + '    '  # 4 spaces between problems
        line2 += operator + ' ' + num2.rjust(max_len - 2) + '    '
        line_equal += '-' * max_len + '    '
        
        # Add the result line if required
        if show_answers:
            line_result += result.rjust(max_len) + '    '
    
    # Combine the lines into the final formatted string
    if show_answers:
        formatted_problems = str(line1 + '\n' + line2 + '\n' + line_equal + '\n' + line_result + '\n')
    else: #if no result is required
        formatted_problems = str(line1 + '\n' + line2 + '\n' + line_equal + '\n')
    
    return formatted_problems


print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))

Press F12 and check a more detailed error in the dev tools console.

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!

1 Like