Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Hello everyone,
I’m encountering an issue with a testing platform I’m using to evaluate a code I’ve developed. I’ve written a function that seems to be working correctly, but the platform is marking some tests as incorrect, even when I believe my code is doing what’s expected of it.

Your code so far

def arithmetic_arranger(problems, show_answers=False):

  contador = 0
  guardado = {}
  for indice, problem in enumerate(problems, start=0):
    contador += 1
    if contador > 4: 
      return print('Error: Too many problems.')
    cadena = ''
    sumando1 = ''
    operador = '+'
    sumando2 = ''
    linea = ''
    tamaño = 0
    for char in problem:
      if char != ' ': 
        cadena += char
    for char in cadena:
      if char.isnumeric():
        sumando1 += char
        if len(sumando1) > 4:
          return print('Error: Numbers cannot be more than four digits.')
      else:
        operador = char
        break
    for char in reversed(cadena):
      if char.isnumeric():
        sumando2 = char + sumando2
        if len(sumando2) > 4:
          return print('Error: Numbers cannot be more than four digits.')
      else:
        break

    if operador == '+':
      resultado = int(sumando1) + int(sumando2)
    else:
      if operador == '-':
        resultado = int(sumando1) - int(sumando2)
      else:
        if operador == '/' or operador == '*':
          return print("Error: Operator must be '+' or '-'.")
        else:
          return print('Error: Numbers must only contain digits.') 

    tamaño = len(sumando1) if len(sumando1) > len(sumando2) else len(sumando2)
    for i in range (tamaño+2):
      if len(sumando1) != tamaño+2:
        sumando1 = ' ' + sumando1
      if len(sumando2) != tamaño+1:
        sumando2 = ' ' + sumando2
      if len(str(resultado)) != tamaño+2:
        resultado = ' ' + str(resultado)

    for j in range (tamaño+2):
      linea += '_'

    guardado[f"variable{indice}"] = sumando1, operador, sumando2, linea, resultado 
                                      #0        #1        #2       #3       #4
 
  if contador == 4:
    if show_answers == False:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '    ' + guardado['variable2'][0] + '    ' + guardado['variable3'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] +'    '+
      guardado['variable2'][1] + guardado['variable2'][2] + '    ' + guardado['variable3'][1] + guardado['variable3'][2] + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '    ' + guardado['variable2'][3] + '    ' + guardado['variable3'][3] + '\n')
    else:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '    ' + guardado['variable2'][0] + '    ' + guardado['variable3'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] +'    '+
      guardado['variable2'][1] + guardado['variable2'][2] + '    ' + guardado['variable3'][1] + guardado['variable3'][2] + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '    ' + guardado['variable2'][3] + '    ' + guardado['variable3'][3] + '\n' + guardado['variable0'][4] + '    '+ guardado['variable1'][4] + '    ' + guardado['variable2'][4] + '    ' + guardado['variable3'][4] + '\n')

  if contador == 3:
    if show_answers == False:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '    ' + guardado['variable2'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] +'    '+
      guardado['variable2'][1] + guardado['variable2'][2] + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '    ' + guardado['variable2'][3] + '\n')
    else:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '    ' + guardado['variable2'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] +'    '+
      guardado['variable2'][1] + guardado['variable2'][2] + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '    ' + guardado['variable2'][3] + '\n' + guardado['variable0'][4] + '    '+ guardado['variable1'][4] + '    ' + guardado['variable2'][4] + '\n')

  if contador == 2:
    if show_answers == False:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] +'    ' + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '\n')
    else:
      print(guardado['variable0'][0] + '    '+ guardado['variable1'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '    ' + guardado['variable1'][1] + guardado['variable1'][2] + '\n'+ guardado['variable0'][3] + '    '+ guardado['variable1'][3] + '\n' + guardado['variable0'][4] + '    '+ guardado['variable1'][4]  + '\n')

  if contador == 1:
    if show_answers == False:
      print(guardado['variable0'][0] + '\n'+ guardado['variable0'][1] + guardado['variable0'][2] + '\n'+ guardado['variable0'][3] + '\n')
    else:
      print(guardado['variable0'][0] + '\n'+ guardado['variable0'][1] + '\n'+ guardado['variable0'][3] + '\n' + guardado['variable0'][4] + '\n')



  
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])
print ('\n')
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
print ('\n')
arithmetic_arranger(["3801 - 2", "123 + 49"])
print ('\n')
arithmetic_arranger(["1 + 2", "1 - 9380"])
print ('\n')
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
print ('\n')
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
print ('\n')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Please format your code properly when you post it, it’s extremely important for languages like Python that rely on indentation

I’ve edited your code for readability. 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 (').

which function? what’s the correct behaviour? what platform, what tests, wrong how?
learning to explain your code is a critical skill

There is a more verbose version of the tests results in the browser console, at a cursory glance many of the tests are receiving None:
AssertionError: None !=

You should check that your function is returning the correct value

Hola josevalmorem

Tu codigo contiene estas lineas:

for j in range (tamaño+2):
      linea += '_'

En lugar de guiones bajos deben ser guiones medios.

Ademas tus resultados contienen espacios al final de cada linea:

  25      25(aqui hay espacios que no deben estar)
+ 15    - 15
----    ----
  40      10

Por ultimo devuelves los resultados con print, modifique un poco tu codigo y cambinado lo que te mencione antes y devolviendo los resultados con return algunas pruebas ya pasan.

return (guardado['variable0'][0] + '\n'+ guardado['variable0'][1] + '\n'+ guardado['variable0'][3] + '\n' + guardado['variable0'][4] + '\n')

saludos

1 Like