Scientific Computing with Python Projects - Arithmetic Formatter

I do not know why my code does not pass the test cases for the show answers = false tests.

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        Errorlength = 'Error: Too many problems.'
        return Errorlength
    problems1=''
    problems2=''
    
    midline = ''
    for problem in problems:
        operand = problem.split(" ")
        if operand[1] not in ['+','-']:
            return "Error: Operator must be '+' or '-'."
        if not operand[0].isdigit() or not operand[2].isdigit():
            return 'Error: Numbers must only contain digits.'
        if len(operand[0]) > 4 or len(operand[2]) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        line = ''
    # Calculate the width needed for the problem
        width = max(len(operand[0]), len(operand[2])) + 2
        for lines in range(width):
            line += '-'
    # Add the formatted operands and operator to the arranged problems string
    
        problems1 += operand[0].rjust(width) + '     '
        problems2 += operand[1] + ' ' + operand[2].rjust(width - 2) + '     '
        midline += line.rjust(width) + '     '
        problems = problems1 + '\n' + problems2 + '\n' + midline + '\n'
        sproblems = ''.join(problems)
    # Remove trailing whitespace from the arranged problems string
        sproblems = sproblems.rstrip()

    # If show_answers is True, calculate and add the answers to the arranged problems
        if show_answers:
            problems += '\n'

            for problem in problems:
                operand[0], operand[1], operand[2] = problem.split()
                if operand[1] == '+':
                    answer = str(int(operand[0]) + int(operand[2]))
                else:
                    answer = str(int(operand[0]) - int(operand[2]))

                width = max(len(operand[0]), len(operand[2])) + 2
                problems += '-' * width + '    '
                problems += answer.rjust(width) + '    '

            sproblems = ' '.join(problems)
        # Remove trailing whitespace from the arranged problems string
            sproblems = sproblems.rstrip()

    # Return the arranged problems
    return sproblems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
1 Like

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

:balloon:Hello @lnneuffer! Welcome to the forum!

It will be much easier to help you if you request help from the challenge step "Help " button as it will take the person helping you directly to the step in question.

Does this help?

Keep up the good progress!

Happy Coding! :slightly_smiling_face:

It looks like you have spacing issues, if you open the browser console you should see:

AssertionError: '  11       3801       1       123          1     \n[91 chars]----' != '  11      3801      1      123         1\n+  4    -[69 chars]----'
-   11       3801       1       123          1     
?     -                - -                  - -----
+   11      3801      1      123         1
- +  4     - 2999     + 2     +  49     - 9380     
?         -          -       -         -      -----
+ +  4    - 2999    + 2    +  49    - 9380
- ----     ------     ---     -----     ------
?     -              -   -         -
+ ----    ------    ---    -----    ------

the lines starting with - are yours, the lines starting with + are the expected

How do I open the browser console

Google would tell you really easily, but F12

It looks like the issue lies in how the function handles the show_answers parameter. When show_answers is False, the function should only arrange the problems without calculating or displaying the answers. However, your code still calculates and adds the answers even when show_answers is False, which is likely causing the failed test cases for that condition.

To fix this, you should separate the logic for arranging the problems and calculating/displaying the answers based on the show_answers parameter. Make sure that when show_answers is False, the function only arranges the problems without calculating or displaying the answers.