Fail message when running py.main on replit

Tell us what’s happening:

Hey,

I am getting an error when running main.py on replit. It says:
" Expected different output when calling “arithmetic_arranger()” with [“3801 - 2”, “123 + 49”] "

the expected output according to test_module.py is ’ 3801 123\n- 2 + 49\n------ -----’

I have counted how many whitespaces there is, the number of dashes etc and it looks the same, so I don’t know what is triggering the fail message.

Here is what it looks like when I run the function with the same argument on pyCharm:

Your code so far

def arithmetic_arranger(problems, display_result=None):

upper_operands = list()
lower_operands = list()
operators = list()
results = list()


if display_result is None:
    display_result = False

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


for operation in problems:

    if '+' in operation:
        operator = '+'
        operators.append(operator)
        operands = operation.split('+')
    elif '-' in operation:
        operator = '-'
        operators.append(operator)
        operands = operation.split('-')
    else:
        return 'Error: Operator must be \'+\' or \'-\'.'

    operands[0] = operands[0].replace(' ','')
    operands[1] = operands[1].replace(' ', '')

    if not operands[0].isdigit() or not operands[1].isdigit():
        return 'Error: Numbers must only contain digits.'
    elif len(operands[0]) > 4 or len(operands[1]) > 4:
        return 'Error: Numbers cannot be more than four digits.'

    upper_operand = operands[0]
    lower_operand = operands[1]

    base_length = max(len(upper_operand), len(lower_operand))


    if len(upper_operand) != base_length:
        diff = base_length - len(upper_operand)
        upper_operand = diff * ' ' + upper_operand
    elif len(lower_operand) != base_length:
        diff = base_length - len(lower_operand)
        lower_operand = diff * ' ' + lower_operand

    upper_operands.append(upper_operand)
    lower_operands.append(lower_operand)

    result = 0

    if display_result is True:
         if operator == '+':
            result = int(upper_operand) + int(lower_operand)
         elif operator == '-':
             result = int(upper_operand) - int(lower_operand)

         diff = base_length - len(str(result)) + 2
         result = diff * ' ' + str(result)
         results.append(result)



upper_row = ''
for num in upper_operands:
    upper_row = upper_row + '  ' + num + '    '
length = len(upper_row)
upper_row = upper_row[0:length-4] + '\n'



middle_row = ''
for num, op in zip(lower_operands, operators):
    middle_row = middle_row + op + ' ' + num + '    '
length = len(middle_row)
middle_row = middle_row[0:length - 4] + '\n'



line = ''
for num in lower_operands:
    for digit in range(len(num) + 2):
        line = line + '-'
    line = line + '    '
length = len(line)
line = line[0:length - 4] + '\n'


formatted_str = upper_row + middle_row + line

if display_result is True:

    bottom_row = ''
    for num in results:
        bottom_row = bottom_row + num + '    '
    length = len(bottom_row)
    bottom_row = bottom_row[0:length - 4] + '\n'

    formatted_str = formatted_str + bottom_row

return formatted_str

print(arithmetic_arranger([‘3801 - 2’, ‘123 + 49’]))

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

can you give the link to your replit or the output of the test?

Yes. jungmatheus (Matheus Jung) - Replit

it seems you have an extra character at the end of your output that shouldn’t be there. the tests say it is a \n

Oh I get it now. The problem was I was I had a ‘\n’ after the dashes even when the second argument was false. It is working now, thank you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.