Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Hi,

Why my code is not accepted and test is still failed?
This code is makeing every point from list.

def arithmetic_arranger(problems, show_answers):

    def split(problems):
        for problem in problems:
            problems = str(problems)
            problem = problems.split(' ')
            return problem

    problem = split(problems)

    def clear_problem(problem):
        cleared_problem = []
        for i in problem:
            clear_trans = str.maketrans({" ": "", "'": "","[":"","]":"",",":""})
            clear_problem = i.translate(clear_trans)
            cleared_problem.append(clear_problem)
        return cleared_problem

    problem = clear_problem(problem)

    def clear_vales(problem):
        values = []
        for i in problem:
            clear_trans = str.maketrans({"+": "", "-": "", "/": "", "*": ""})
            clear_values = i.translate(clear_trans)
            values.append(clear_values)
            clear_values_list = list(filter(None, values))
        return clear_values_list

    clear_values_list = clear_vales(problem)

    def clear_signs(problem):
        values = []
        for i in problem:
            clear_trans = str.maketrans(
        {' ': '', '0': '', '9': '', '8': '', '7': '', '6': '', '5': '', '4': '', '3': '', '2': '', '1': ''})
            clear_values = i.translate(clear_trans)
            values.append(clear_values)
            clear_values_list = list(filter(None, values))
        return clear_values_list

    clear_signs_list = clear_signs(problem)

    def class_dig(clear_values_list):
        class_dig = []
        for i in clear_values_list:
            letters_trans = str.maketrans(
                {'0': None, '9': None, '8': None, '7': None, '6': None, '5': None, '4': None, '3': None, '2': None, '1': None, '/':None, '+':None, '-':None})
            letters = i.translate(letters_trans)
            class_dig.append(letters)
        return list(filter(None, class_dig))

    letters = class_dig(clear_values_list)

    first_values = clear_values_list[0::2]
    second_values = clear_values_list[1::2]

    res = []
    for i in range(len(first_values)):
        first = first_values[i]
        second = second_values[i]
        if show_answers == False:
           res.append(' ')
        elif clear_signs_list[i] == '+':
            res.append(int(first) + int(second))
        elif clear_signs_list[i] == '-':
            res.append(int(first) - int(second))
        else:
            res.append(' ')

    dots = []
    for i in range(len(first_values)):
        first = first_values[i]
        second = second_values[i]
        if len(first) > len(second):
            dots.append(((len(first)) + 2) * '-')
        elif len(first) < len(second):
            dots.append(((len(second)) + 2) * '-')
        else:
            dots.append(((len(second)) + 2) * '-')

    spaces_first = []
    spaces_second = []
    spaces_rest = []

    for i in range(len(first_values)):
        first = first_values[i]
        second = second_values[i]
        rest = str(res[i])
        dot = dots[i]
        if len(first) > len(second):
            spaces_second.append((len(first) - len(second)) * ' ')
            spaces_first.append('  ')
        elif len(first) < len(second):
            spaces_first.append(((len(second) - len(first)) + 2) * ' ')
            spaces_second.append('')
        else:
            spaces_first.append('  ')
            spaces_second.append('')

        spaces_rest.append((len(dot) - len(rest)) * ' ')

        leng = []
    for i in problem:
        leng.append(len(i))


    for i in problem:
        if max(leng) > 4:
            print('Error: Numbers cannot be more than four digits.')
            break
        if len(problem) > 15:
            print('Error: Too many problems.')
            break
        for i in clear_signs_list:
            if i == '/':
                return print("Error: Operator must be '+' or '-'.")
            break
        for i in letters:
            if len(letters) > 0:
                return print('Error: Numbers must only contain digits.')
            break
        if len(problem) == 3:
            return print(f'{spaces_first[0]}{first_values[0]} \n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} \n{dots[0]} \n{spaces_rest[0]}{res[0]}')
        if len(problem) == 6:
            return print(f'{spaces_first[0]}{problem[0]}    {spaces_first[1]}{first_values[1]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]}    {clear_signs_list[1]} {spaces_second[1]}{second_values[1]}\n{dots[0]}    {dots[1]}\n{spaces_rest[0]}{res[0]}    {spaces_rest[1]}{res[1]}')
        if len(problem) == 9:
            return print(f'{spaces_first[0]}{problem[0]}    {spaces_first[1]}{first_values[1]}    {spaces_first[2]}{first_values[2]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]}    {clear_signs_list[1]} {spaces_second[1]}{second_values[1]}    {clear_signs_list[2]} {spaces_second[2]}{second_values[2]}\n{dots[0]}    {dots[1]}    {dots[2]}\n{spaces_rest[0]}{res[0]}    {spaces_rest[1]}{res[1]}    {spaces_rest[2]}{res[2]}')
        if len(problem) == 12:
            return print(f'{spaces_first[0]}{problem[0]}    {spaces_first[1]}{first_values[1]}    {spaces_first[2]}{first_values[2]}    {spaces_first[3]}{first_values[3]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]}    {clear_signs_list[1]} {spaces_second[1]}{second_values[1]}    {clear_signs_list[2]} {spaces_second[2]}{second_values[2]}    {clear_signs_list[3]} {spaces_second[3]}{second_values[3]}\n{dots[0]}    {dots[1]}    {dots[2]}    {dots[3]}\n{spaces_rest[0]}{res[0]}    {spaces_rest[1]}{res[1]}    {spaces_rest[2]}{res[2]}    {spaces_rest[3]}{res[3]}')
        if len(problem) == 15:
            return print(f'{spaces_first[0]}{problem[0]}    {spaces_first[1]}{first_values[1]}    {spaces_first[2]}{first_values[2]}    {spaces_first[3]}{first_values[3]}    {spaces_first[4]}{first_values[4]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]}    {clear_signs_list[1]} {spaces_second[1]}{second_values[1]}    {clear_signs_list[2]} {spaces_second[2]}{second_values[2]}    {clear_signs_list[3]} {spaces_second[3]}{second_values[3]}    {clear_signs_list[4]} {spaces_second[4]}{second_values[4]}\n{dots[0]}    {dots[1]}    {dots[2]}    {dots[3]}    {dots[4]}\n{spaces_rest[0]}{res[0]}    {spaces_rest[1]}{res[1]}    {spaces_rest[2]}{res[2]}    {spaces_rest[3]}{res[3]}    {spaces_rest[4]}{res[4]}')


arithmetic_arranger(["3801 - 2", "123 + 49"],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 Edg/131.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Which tests are you failing?

What does your output look like?

Did you check the dev tools browser console? (F12)

  1. Build an Arithmetic Formatter Project

  2. my output looks like typical fraction

  3. in dev i see 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 != ’ 32 1 45 123 98[126 chars]1028’
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at (python-test-evaluator.ts:90:13), :3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)

This error is important. It compares your output with the expected output. Your output comes first:

 None

So your function is not returning anything. You need to return the results, not print it. And don’t return a print call.

If you want to see the results call your function like this instead:

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

Thank You :+1:

it work :grinning:

2 Likes