Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I completed this challenge and my output is exactly as required, but when I run the tests it gives the message “tests completed” but each test shows an X besides it and I’m not able to mark this challenge as completed.

Your code so far

def arithmetic_arranger(problems, show_answers=False):  
    output = {'line1':'', 'line2':'', 'line3': '', 'line4': ''}
    if len(problems) > 5:
        return print('Error: Too many problems.')
          
    for problem in problems:
        
        if '+'  in problem:
            operator = problem[problem.index('+')]      
            
        elif '-' in problem:
            operator = problem[problem.index('-')]
            
        else:
            return print('"Error: Operator must be \'+\' or \'-\'."')
            
        operand1 = problem[:problem.index(operator)].strip()        
        operand2 = problem[problem.index(operator) + 1:].strip()
        if not operand1.isnumeric() or not operand2.isnumeric():
            return print('Error: Numbers must only contain digits.')
        if len(operand1) > 4 or len(operand2) > 4:
            return print('Error: Numbers cannot be more than four digits.')
        length1 = len(operand1)
        length2 = len(operand2)
        if length1 > length2:
            longest = length1
        else:
            longest = length2
        
        if operator == '+':
            answer = int(operand1) + int(operand2)
        else:
            answer = int(operand1) - int(operand2)

        line1 =  operand1.rjust(longest + 2)               #line 1
        line2 =  operator + ' ' + operand2.rjust(longest)  #line 2
        line3 = '-' * (longest + 2)                        #line 3
        line4 =  str(answer).rjust(longest + 2)            #line 4
        output['line1'] += line1 + ' ' * 4
        output['line2'] += line2 + ' ' * 4
        output['line3'] += line3 + ' ' * 4
        output['line4'] += line4 + ' ' * 4


    print(output['line1'])
    print(output['line2'])
    print(output['line3'])
    if show_answers: print(output['line4']) 
    
arithmetic_arranger(["3801 - 2", "123 + 49"])

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

open the browser console with F12, you can see a more detailed output of the tests.

For example

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [100 chars]    '
             != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'

the upper line is yours, the lower is the expected. You have extra spaces before each new line character

Thanks for replying, Now I explicitly stripped off all whitespace at the end of each line but it’s still not passing the tests even though the output is exactly what’s required.

def arithmetic_arranger(problems, show_answers=False):  
    output = {'line1':'', 'line2':'', 'line3': '', 'line4': ''}
    if len(problems) > 5:
        return print('Error: Too many problems.')
          
    for problem in problems:
        
        if '+'  in problem:
            operator = problem[problem.index('+')]      
            
        elif '-' in problem:
            operator = problem[problem.index('-')]
            
        else:
            return print('"Error: Operator must be \'+\' or \'-\'."')
            
        operand1 = problem[:problem.index(operator)].strip()        
        operand2 = problem[problem.index(operator) + 1:].strip()
        if not operand1.isnumeric() or not operand2.isnumeric():
            return print('Error: Numbers must only contain digits.')
        if len(operand1) > 4 or len(operand2) > 4:
            return print('Error: Numbers cannot be more than four digits.')
        length1 = len(operand1)
        length2 = len(operand2)
        if length1 > length2:
            longest = length1
        else:
            longest = length2
        
        if operator == '+':
            answer = int(operand1) + int(operand2)
        else:
            answer = int(operand1) - int(operand2)

        line1 =  operand1.rjust(longest + 2) + ' ' * 4  #line 1
        line2 =  operator + ' ' + operand2.rjust(longest) + ' ' * 4   #line 2
        line3 = '-' * (longest + 2) + ' ' * 4  #line 3
        line4 =  str(answer).rjust(longest + 2)  #line 4
        output['line1'] += line1 
        output['line2'] += line2 
        output['line3'] += line3 
        output['line4'] += line4
    output['line1'].strip()
    output['line2'].strip()
    output['line3'].strip()
    output['line4'].strip()

    if show_answers:
        
        print(
            output['line1'] + '\n' + output['line2'] + '\n' + output['line3'] + '\n' + output['line4']   
            )   
    
    else:
        print(
            output['line1']+'\n'+output['line2']+'\n'+output['line3']
            )

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

Can you please share the detailed error output from the browser console? (F12 to access)

Hi. got the same problem as you, fixed it by doing this:

removed

our problem sits on the fact that we have an empty space (4 spaces after each problem) on the end of each formatted problem. the test wants us to add the spaces at the start of the formatted problem.

what i did was removing all spaces on the end of the first problem (index == 0) and adding a space at the start of the other problems. kinda hard to explain it here. but feel free to ask

could actually simplify the code like this:

removed

Hi and welcome to the forum :wave:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

in the latest code the errors are

AssertionError: None 
             != '    3      988\n+ 855    +  40\n-----    -----\n  858     1028

You still need to return the final value

My apologies, first time here :smile:

1 Like

Hi.

If you refer to @im59138’s reply, you should be getting an AssertionError:

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [100 chars]    '
             != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'

Note that the first argument is YOUR answer, which results in 100 chars, whereas the test wants you to get 84 chars. Take note of the 4 block space between the problems, the assertion error is caused because of the 4 spaces are put at the end of the problem. You should put them at the start.

1 Like

This is interesting, now I added the spaces before the operands and the output seems to be what’s expected, but now the console is showing this error:

python-test-evaluator.ts:177 PythonError: Traceback (most recent call last):
File “/lib/python311.zip/_pyodide/_base.py”, line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File “/lib/python311.zip/_pyodide/_base.py”, line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “”, line 4, in
File “/lib/python311.zip/unittest/case.py”, line 873, in assertEqual
assertion_func(first, second, msg=msg)
File “/lib/python311.zip/unittest/case.py”, line 866, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: None != ’ 3801 123\n- 2 + 49\n------ -----’

And here is my code:
def arithmetic_arranger(problems, show_answers=False):
output = {‘line1’:‘’, ‘line2’:‘’, ‘line3’: ‘’, ‘line4’: ‘’}
if len(problems) > 5:
return print(‘Error: Too many problems.’)
index = 0
for problem in problems:
if index > 0:
seperator_spaces = ’ ’ # four spaces here
else:
seperator_spaces = ‘’ # zero spaces here

    if '+'  in problem:
        operator = problem[problem.index('+')]      
        
    elif '-' in problem:
        operator = problem[problem.index('-')]
        
    else:
        return print('"Error: Operator must be \'+\' or \'-\'."')
        
    operand1 = problem[:problem.index(operator)].strip()        
    operand2 = problem[problem.index(operator) + 1:].strip()
    if not operand1.isnumeric() or not operand2.isnumeric():
        return print('Error: Numbers must only contain digits.')
    if len(operand1) > 4 or len(operand2) > 4:
        return print('Error: Numbers cannot be more than four digits.')
    length1 = len(operand1)
    length2 = len(operand2)
    if length1 > length2:
        longest = length1
    else:
        longest = length2
    
    if operator == '+':
        answer = int(operand1) + int(operand2)
    else:
        answer = int(operand1) - int(operand2)

    line1 =  seperator_spaces + operand1.rjust(longest + 2)               #line 1
    line2 =  seperator_spaces + operator + ' ' + operand2.rjust(longest)  #line 2
    line3 = seperator_spaces + '-' * (longest + 2)                        #line 3
    line4 =  seperator_spaces + str(answer).rjust(longest + 2)            #line 4
    output['line1'] += line1 
    output['line2'] += line2 
    output['line3'] += line3 
    output['line4'] += line4 
    index += 1


print(output['line1'])
print(output['line2'])
print(output['line3'])
if show_answers: print(output['line4']) 

arithmetic_arranger([“3801 - 2”, “123 + 49”])

Your function is returning “None”

1 Like

Hi @gtrrich, I was in a similar situation as you and couldn’t figure out why my code passed all the tests but was giving X’s for every one.
I got it to pass. Turns out the challenge is asking for code that returns results and error codes, but does not print anything.
Hope this helps. I’m new to programming and this point was very confusing.

Woohoo!! it worked all my tests passed, thank you all for your guidance!

excuse me, can you please show you the code that you finished all the guidance???

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.