Do it correct and fail the test

i can see that the results and the print match perfec but says that are bad displayed can some one explain why and say me how to correct the code and the proyec on repl are down here

def arithmetic_arranger(problems, responder=False):
    nums = ''
    dens = ''
    sums = ''
    respuestas = ""
    if len(problems) > 5:
        return 'Error: Too many problems.'
    for k in problems:
        l = (k.split(' '))
        num = l[0]
        op = l[1]
        den = l[2] 
        if len(num) > 4 or len(den) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if not num.isnumeric() or not den.isnumeric():
            return 'Error: Numbers must only contain digits.'
        if op == '+' or op == '-':
            length = max(len(num), len(den)) + 2
            top = str(num).rjust(length)
            bottom = op + str(den).rjust(length - 1)
            sum = ''
            for s in range(length):
                sum += '-'
            nums += top + '    '
            dens += bottom + '    '
            sums += sum + '    '
        else:
            return "Error: Operator must be '+' or '-'."
    nums = nums.rstrip()
    dens = dens.rstrip()
    sums = sums.rstrip()
    if responder:
        for k in problems:
          l = (k.split(' '))
          num = l[0]
          op = l[1]
          den = l[2]
          respuesta = ""
          if op == "+" :
            respuesta = (int(num) + int(den))
          else:
            respuesta = (int(num) - int(den))
          length = max(len(num), len(den)) + 2
          top = str(respuesta).rjust(length)
          respuestas += top + '    '
          respuestas.rstrip()
          arranged_problems = nums + '\n' + dens + '\n' + sums 
        return(arranged_problems) + '\n'+ respuestas
    else:    
        arranged_problems = nums + '\n' + dens + '\n' + sums 
        return (arranged_problems)

https://repl.it/@kapox100/RosySquareBrace

Hello!

Your code is returning extra characters on line with the results of the operations.

When you run the tests it shows the following (I formatted it a little better here, so it will differ):

AssertionError:
'   3[72 chars] ------    ----    -----\n -666     -3800      88      172    ' 
!= 
'   3[72 chars] ------    ----    -----\n -666     -3800      88      172'

The first string (before the !=) is the result from your script and the second one is the expected output. As you can see, there are 4 extra white spaces after the 172 on your result.

God, the mistake was that I made respuestas.rstrip() and the correct one was respuestas = respuestas.rstrip() Thank you very much for your help. I literally got it a week ago and can’t figure out why give the error.

1 Like