Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

what do i need to pass the first test, it’s not clear

Your code so far

def rules(operators):
    if operators == "+" or operators == "-":
        print("okay")
    else:
        print("Error: Operator must be '+' or '-'.") 
        

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print("Error: Too many problems")
    
    return problems

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

if you look at the browser’s console window (f12) you can see detailed error messages. If you can’t interpret them for yourself, please post them here for help.

wait you mean this?


please tell me what this means i’ve finished the course and didn’t use this f12 thing? what actually is it? And how come there isn’t an error on the terminal?

f12 opens the browser console. I’m not sure what the course teaches as I haven’t gone thru it but the console shows errors in websites for eg.

Umm, is this screenshot taken after you clicked Run or before?
(can you try run again and see if it logs anything?)

yes it does i think i’ve solved it

1 Like

The assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'

Your output comes first, and the output that the test expects is second.

Your output: Year does not equal what’s expected: Years

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------    -----'

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.

I hope this helps interpret your error!

Your function is returning a list, not a string formatted as the example.

You can use repr() to see the formatting of your string as it shows in the test.

print(repr(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])))

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))

You should probably test using both like this so you can see the formatted and raw strings.

1 Like

oh okay that clears up the problem more

what? my code isn’t wrong yet its isn’t being applied?

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print("Error: Too many problems")
    if problems == "+":
        pass

    if problems == "-":
        pass
    else:
        print("Error: Operator must be '+' or '-'.")

    return problems

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

i’ve put a * in there
EDIT: forget this i’ve figured out the logic behind my code is wrong majorly good thing im reviewing this

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.

return, not print

how do i explain code to myself for it to be effective and for me to see logic errors? I don’t want to just rely on you guys. When i explain it i just say what the code does, for example in my code i import regex to see a pattern in the code defining a function which has problems and show answer = false as the arguments

have you ever considered learning to use a debugger?

also, there are tools online to help you debug regex patterns. (like regexr.com but be sure to set it up for the programming language you need it for)

1 Like

print statements can help with this because you can visualize what’s happening with variables or narrate the code to yourself this way.

You can also use a debugging tool as @hbar1st mentioned, something like this:
https://pythontutor.com/python-compiler.html#mode=edit

which you can use to step through the code slowly and see what’s happening.

1 Like

the ai on that link is crazy it responds like how you guys respond, it doesn’t give the answer it just questions lines of code and asks you what the logic behind it is

also could i ask why none of my stuff is passing the tests even tho i’m doing them??

import re

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return("Error: Too many problems")
    for problem in problems:
        print("this", problem)
        parts = problem.split()  
        operand = problem.split()
    
        
        operator = parts[1]
        

        print("this", operand)
        if operator != "+" and operator != "-":
            print("Error: Operator must be '+' or '-'.")

        
        if re.search(r'\D', operand[0]) or re.search(r'\D', operand[2]):
            print("Error: Numbers must only contain digits.")
 
        if len(operand[0]) > 4 or len(operand[2]) > 4:
            print('Error: Numbers cannot be more than four digits.')    

    return problems

             
           
print(repr(f'\n{arithmetic_arranger(["32345 * 698", "3801 - 2", "45 + 43", "123 + 49"])}'))
print(repr(f'\n{arithmetic_arranger}(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"]'))

i should be passing 5,6,7 by now

should you? what does your function return?

oh it doesn’t return them it prints them… but how can i return all those print statements?

don’t return print statements, return strings

yeah but how could i do that if i to more then one return say replace the first error for return instead of print it will just stop the loop

Yes. if the input is not valid, why continue?