Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

Hello! I am performing the “arithmetic_arranger”. I made the following code but it doesn’t meet the tests. In my code editor it works correctly but the evaluation platform tells me in any option that I should do something else. For example:

arithmetic_arranger([“3801 - 2”, “123 + 49”]) should return 3801 123\n- 2 + 49\n------ -----.

Sorry for my English, I’m not native. I speak Spanish

Your code so far

def arithmetic_arranger(problems):
    salida = ["", "", "", ""]

    if len(problems) > 5:
        return('Error: Too many problems.')

    for operacion in problems:
        if "+" in operacion:
            op_1, operador, op_2 = operacion.partition('+')
            resultado = int(op_1) + int(op_2)

        elif "-" in operacion:
            op_1, operador, op_2 = operacion.partition('-')
            resultado = int(op_1) - int(op_2)
        else:
            return ("Error: Operator must be '+' or '-'.")

        op_1 = op_1.strip()
        op_2 = op_2.strip()

        if not (op_1.isdigit() and op_2.isdigit()):
            return 'Error: Numbers must only contain digits.
        if len(op_1) > 4 or len(op_2) > 4:
            return 'Error: Numbers cannot be more than four digits.
        
        maxLen = max(len(op_1), len(op_2))
        salida[0] += op_1.rjust(maxLen + 2) + "    "
        salida[1] += operador + " " + op_2.rjust(maxLen) + "    "
        salida[2] += "-" * (maxLen + 2) + "    "
        salida[3] += str(resultado).rjust(maxLen + 2) + "    "

    return "\n".join(salida)

print (arithmetic_arranger(["3801 - 2", "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/121.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Take the input it mentions and put it in your function call and see how the result is different.

You can use repr to see the raw string format as it displays here.

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

This line has an error that prevents any code from running.

This one as well

yes, Yes, it was a “copying problem” but in my code it is fine, with a (')

1 Like

Did you try running this at the end of your code?

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

Yes, te output is

’ 11 3801 1 123 1 \n+ 4 - 2999 + 2 + 49 - 9380 \n---- ------ — ----- ------ \n’

but, continues in ‘Error’ in FreecodeCamp plataform. (the program function ok)

arithmetic_arranger([“3801 - 2”, “123 + 49”]) should return 3801 123\n- 2 + 49\n------ -----.
’ 11 3801 1 123 1 \n+ 4 - 2999 + 2 + 49 - 9380 \n---- ------ — ----- ------ \n’

If this was your output something is very wrong!

The output that I got with your code is this:

'  3801      123    \n-    2    +  49    \n------    -----    \n  3799      172    '

but the error says it should be this:

3801 123\n- 2 + 49\n------ -----

You need to find out why

You have also not implemented this:

The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.

You are printing the answers to the problems when you should only be formatting them as vertical.

You can test each test data like this:

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

Test each one and see your result.

the output in my code editor is

Use this to call your function to test this problem:

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

You should not be displaying the answers to the questions.

You need to implement this:

The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.

Focus on this feedback/hint first:

arithmetic_arranger([“3801 - 2”, “123 + 49”]) should return 3801 123\n- 2 + 49\n------ -----.

It is a complex situation, since the statement of the exercise does ask me for the result (and the [show anwers is optional[) but in that example the result should be shown, since it meets the conditions. That’s why my program shows it. (I have tried applying # to the output cell of the result, so this way it skips it, but it also shows the same error) Applying the repr function, here is the output.

in Freecodecamp :

This is not correct. You only show the answer if the second argument is “True”, which you havent implemented. It’s optional to supply the second function when calling your function but you must implement it

The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.

If the function call opts not to supply the argument it would default to “False”. That’s what makes it optional. If it’s not “True” then you should not show the answer.

The test is telling you exactly what the output should be, believe it.

okey!
tienes razon, habia comprendido mal el ejercicio.
Use el valor booleano [mostrar respuestas], lo configure para que luego de las validaciones de error, se vuelva TRUE .

elimine. las sentencias del resultado de operadores.

Ahora en la prueba, cumple con todas las validades cuando tiene un error pero con las operaciones validas, me indica lo mismo que antes en la web de Freecodecamp (ejemplo: arithmetic_arranger([“3801 - 2”, “123 + 49”]) should return 3801 123\n- 2 + 49\n------ -----.
)

def arithmetic_arranger(problems, show_answers=False):
    salida = ["", "", "", ""]

    if len(problems) > 5 and  show_answers==False:
        return('Error: Too many problems.')

    for operacion in problems:
        if "+" in operacion:
            op_1, operador, op_2 = operacion.partition('+')
            operador='+'

        elif "-" in operacion:
            op_1, operador, op_2 = operacion.partition('-')
            operador='-'
            #resultado = int(op_1) - int(op_2)

        else:
            show_answers=False
            return ("Error: Operator must be '+' or '-'.")

        op_1 = op_1.strip()
        op_2 = op_2.strip()

        if not (op_1.isdigit() and op_2.isdigit()):
            show_answers = False
            return "Error: Numbers must only contain digits."
        if len(op_1) > 4 or len(op_2) > 4:
            show_answers = False
            return "Error: Numbers cannot be more than four digits."

        show_answers=True
        maxLen=max(len(op_1),len(op_2))

        salida[0]+=op_1.rjust(maxLen + 2) + "    "
        salida[1]+= operador+ " " +  op_2.rjust(maxLen)+ "    "
        salida[2]+= "-" * (maxLen + 2) + "    "

    if show_answers==True:
        return ("\n".join(salida))


output with “repr” :


output “print” :
Captura de pantalla 2024-03-13 a la(s) 15.36.50

1 Like

Your result looks much better :+1:

'  3801      123    \n-    2    +  49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

But… it needs to look exactly the same. Same spaces and without '\n' at the end

1 Like

Do you have any ideas on how to approach the problem of that modification?
Is rjust correct from your point of view? or what tools could I use?

these spaces at the end should not be added for the last problem

or you should remove the spaces before concatenating the lines together

What the other user above said is that what is expected is one thing and what I present is another. Here is a photo of those two exits (the first is mine and the second is the expected one)

When I do what you tell me, eliminating the spaces at the end, the result is this:

I want to tell you that when I execute the output (without Repr), the outputs (vertical arithmetic problem)
They are identical but the platform keeps giving me an error:

print((arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])))

Captura de pantalla 2024-03-13 a la(s) 17.33.05

and
print ('\n 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------.')

even if you don’t see them, the spaces are characters, and if you have spaces that shouldn’t be there the two strings are not equal
like

"gelato" == "gelato        " 

is False, if you print them tho

>>> print("gelato")
gelato
>>> print("gelato        ")
gelato        

but the spaces are there anyway

You’re adding spaces here before the newline, and you should not.

You are adding 4 spaces on each operacion

for operacion in problems:
        salida[0]+=op_1.rjust(maxLen + 2) + "    "
        salida[1]+= operador+ " " +  op_2.rjust(maxLen)+ "    "
        salida[2]+= "-" * (maxLen + 2) + "    "
        print(repr(salida))

But on the last operacion you do not want to add the spaces.

Screenshot 2024-03-14 085843

  1. On the first loop, the first space is added :white_check_mark:
  2. On the second loop, the second space is added :x:

i dont know How could I control that aspect. Is it possible that the problem is Rjust? Or could I write a conditional inside the loop indicating that if it is the last turn, apply rstrip?

Thank you for your help, I applied this code at the end, (I know it may be very primitive or rustic but I am still very new) and it worked correctly. I found another error in the code regarding showing the results but I can solve that

1 Like