Hi,
I’m stuck with this project. I can’t see the error and why I’m not able to pass the tests, since everything seems to be correct.
You can see the project here. https://repl.it/join/oschujoz-robertogimenez
Please see my code.
def arithmetic_arranger(problems, other=False):
primera_fila = ""
segunda_fila = ""
tercera_fila = ""
resultado = ""
arranged_problems = ""
#print(problems)
if (len(problems) > 5):
arranged_problems = "Error: Too many problems."
else:
espacios = ""
for i, problema in enumerate(problems):
p = problema.split()
try:
n1 = int(p[0])
n2 = int(p[2])
except:
arranged_problems = "Error: Numbers must only contain digits."
return arranged_problems
max_len = max(len(p[0]), len(p[2]))
if (max_len > 4):
arranged_problems = 'Error: Numbers cannot be more than four digits.'
return arranged_problems
#print(p)
if ((p[1] == '+') or (p[1] == '-')):
if (i != 0):
espacios = " "
if (p[1] == '+'):
suma = n1 + n2
esp = max_len + 2 - len(str(suma))
resultado = resultado + espacios + esp * " " + str(suma)
else:
resta = n1 - n2
esp = max_len + 2 - len(str(resta))
resultado = resultado + espacios + esp * " " + str(resta)
#print(resultado)
if ((len(p[0]) != max_len) or (len(p[0]) == len(p[2]))):
primera_fila = primera_fila + espacios + (
" " + (max_len - len(p[0])) * " " + str(p[0]))
segunda_fila = segunda_fila + espacios + (str(p[1]) + " " +
str(p[2]))
else:
primera_fila = primera_fila + espacios + (" " + str(p[0]))
segunda_fila = segunda_fila + espacios + (
str(p[1]) + " " +
(max_len - len(p[2])) * " " + str(p[2]))
tercera_fila = tercera_fila + espacios + ((2 + max_len) * '-')
else:
arranged_problems = "Error: Operator must be '+' or '-'."
return arranged_problems
arranged_problems = primera_fila + '\n' + segunda_fila + '\n' + tercera_fila + '\n' + resultado + '\n'
#print(arranged_problems)
#print('fin')
return arranged_problems
I’m passing the first test, but the second one is raising an error.
It’s weird that some strange chars are appearing after the second and third tests, like ‘F…F’
Any ideas?
Thanks!