Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code runs perfectly in VSCode and in the console in freecodecamp yet I keep getting the error: Your code raised an error before any tests could run. Please fix it and try again. Any help would be appreciated

Your code so far

def arithmetic_arranger(problems, show_answers=True):
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""

    if len(problems) > 5:
        raise ValueError("Error: Too many problems.")
    
    for item in problems:
        #splitting items
        splitItem = item.split(" ")
        num1 = int(splitItem[0])
        operand = splitItem[1]
        num2 = int(splitItem[2])
        maxLen = max(len(str(num1)),len(str(num2)))

        #error handling
        if operand != "+" and operand != "-":
            raise ValueError("Error: Operator must be '+' or '-'.")
        if type(num1) != int or type(num2) is not int:
            raise ValueError('Error: Numbers must only contain digits.')
        if len(str(num1)) > 4 or len(str(num2)) > 4:
            raise ValueError('Error: Numbers cannot be more than four digits.')
        #making the lines
        line1 = line1 + f"{num1:>{maxLen + 2}}    " 
        line2 = line2 + f"{operand} {str(num2):>{maxLen}}    "
        line3 = line3 + "-" * (maxLen + 2) + "    "  
        #checking if show answers is true
        if show_answers and operand == "+":

            line4 = line4 + f"{(num1 + num2):>{maxLen+2}}    "
        elif show_answers and operand == "-":
            line4 = line4 + f"{(num1 - num2):>{maxLen+2}}    "
    lines = [
        line1.rstrip(),
        line2.rstrip(),
        line3.rstrip(),
    ]
    if show_answers:
        lines.append(line4.rstrip())

    problems = "\n".join(lines)
    return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I think these count as errors

double check what you are expected to do in these cases

It just says, " The function will return the correct conversion if the supplied problems are properly formatted, otherwise, it will return a string that describes an error that is meaningful to the user."

and is it what you are doing? returning a string?

I’ve just changed it to return a string. My new code

def arithmetic_arranger(problems, show_answers=True):
    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""

    if len(problems) > 5:
        return "Error: Too many problems."
    
    for item in problems:
        #splitting items
        splitItem = item.split(" ")
        num1 = int(splitItem[0])
        operand = splitItem[1]
        num2 = int(splitItem[2])
        maxLen = max(len(str(num1)),len(str(num2)))

        #error handling
        if operand != "+" and operand != "-":
            return "Error: Operator must be '+' or '-'."
        if type(num1) != int or type(num2) is not int:
            return 'Error: Numbers must only contain digits.'
        if len(str(num1)) > 4 or len(str(num2)) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        #making the lines
        line1 = line1 + f"{num1:>{maxLen + 2}}    " 
        line2 = line2 + f"{operand} {str(num2):>{maxLen}}    "
        line3 = line3 + "-" * (maxLen + 2) + "    "  
        #checking if show answers is true
        if show_answers and operand == "+":

            line4 = line4 + f"{(num1 + num2):>{maxLen+2}}    "
        elif show_answers and operand == "-":
            line4 = line4 + f"{(num1 - num2):>{maxLen+2}}    "
    lines = [
        line1.rstrip(),
        line2.rstrip(),
        line3.rstrip(),
    ]
    if show_answers:
        lines.append(line4.rstrip())

    problems = "\n".join(lines)
    return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

do you need more help now?

Yes it is still not working

I’ve edited your post to improve the readability of the code. 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 (').

what about are you getting now? still the same?

same error. Thank you for adding readability!

I’ve also tried to refresh the cache and all the usual steps

open the browser console, there will be a more detailed output from the tests that you can use as a guide to fix things

No errors. Should I copy and paste my console?

it appears there when you run the tests

Sorry what do you mean?

when you press the Run the tests you will get the more detailed output in the browser console, if you look at the browser console before running the tests you will not see the errors

Yeah I did it after I ran the tests. No errors.
Actually the one error isCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at tps://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015. (Reason: CORS request did not succeed). Status code: (null).

None of the “sha512” hashes in the integrity attribute match the content of the subresource at ttps://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015”. The computed hash is “z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==”.

uh, that is an unexpected issue

what browser are you using?

Mozzila firefox. Note these errors were from BEFORE I ran the tests. Also, I have triecd on a different computer and a different browser

Except for that there are no errors. The code seems to run well and all the tests return the right result in vscode. Hey I can’t type any more due to being a new users is there any way I could talk to you without waiting 24 h
Ok here are the errors in google chrome:
`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 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File “/lib/python311.zip/unittest/case.py”, line 703, in fail
raise self.failureException(msg)
AssertionError: ’ 3801 123\n- 2 + 49\n------ -----\n 3799 172’ != ’ 3801 123\n- 2 + 49\n------ -----’
3801 123

  • 2 + 49

? -

  • ------ ------ 3799 172

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module._pythonexc2js (pyodide.asm.js:9:654797)
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at target.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.js:2:152346)
    at test (eval at (python-test-evaluator.js:2:151978), :3:5)
    at #i (python-test-evaluator.js:2:152879)
    at async PythonTestEvaluator.handleMessage (python-test-evaluator.js:2:153663)
    #i @ python-test-evaluator.js:2
    await in #i
    handleMessage @ python-test-evaluator.js:2
    onmessage @ python-test-evaluator.js:2Understand this error
    `

and Failed to load resource: the server responded with a status of 401 ()

If you could send me a message when you find anything please tell me