Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Please help me, I didn’t find my false. What do I have to do? Where is my false?

As you see my code, I wrote everything, and I tried, everything is running ohne problem. But system said me 1,2,3,4,9 and 10 is false.

Your code so far

def arithmetic_arranger(digits, show_results=False):
    # Making arrangement
    first_line = []
    second_line = []
    dashes = []
    results = []
    if len(digits) > 5:
        return 'Error: Too many problems.'
    for digit in digits:
        # Split digits
        parts = digit.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        # Valid input situations
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        if not (num1.isdigit() and num2.isdigit()):
            return 'Error: Numbers must only contain digits.'
        if len(num1) > 4 or len(num2) > 4:
            return 'Error: Numbers cannot be more than four digits.'

        # Deciding width
        width = max(len(num1), len(num2)) + 2

        # Shaping digits in a format
        first_line.append(num1.rjust(width))
        second_line.append(operator + ' ' + num2.rjust(width - 2))
        dashes.append('-' * width)

        # Result display
        if show_results:
            if operator == '+':
                result = str(int(num1) + int(num2))
            else:
                result = str(int(num1) - int(num2))
            results.append(result.rjust(width))
    # Combine the lines into a single output
    arranged_digits ='\n' + '    '.join(first_line) +'\n' +'    '.join(second_line) + '\n' +'    '.join(dashes)
    if show_results:
        arranged_digits +='\n' +'    '.join(results)
    return arranged_digits
# Test the function
# problem1 = 
print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
# problem2 = 
print(arithmetic_arranger(["1 + 2", "1 - 9380"]))
# problem3 = ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
# problem4 = ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
# problem9 = 
print(arithmetic_arranger(["3 + 855", "988 + 40"],True))
# problem10 = ["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"]
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"],True))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

press F12 to open the browser console, you will see a more detailed output of 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!

Try using repr() to see the raw string formatting to compare it better to the tests:

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))