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:
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.
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))
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)
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:
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