Arithmetic Formatter Formatting Error

Formatting Errors on Replit but the code works on my IDE.

I was wondering if anyone had any advice on where to go with my code for this problem? The code works when it is run through my IDE but I am having issues getting it to print properly in replit. If anyone has any tips on how to correct this error it is greatly appreciated. I am not lookin for the exact answer necessarily, but just some general idea of what is wrong with the code.

Edit I also am noticing an issue when when the input contains “True” I am getting a rejection. I believe I know how to correct this issue. My one question would be that on the challenge page the Inputs are located within a ( ) but the test cases are in . I imagine the cost differs based on or ().

Thank you!

def arithmetic_arranger(problems, bvalue = False):
first_num =
second_num =
dashes =
ans =
for x in problems:
x = x.split()
y = ’ '.join(problems)
y = y.split()
y = sorted(y, key=len, reverse=True)

    if len(problems) > 5:
        return 'Error: Too many problems.'
    if x[1] != '+' and x[1] != '-':
        return('Error: Operator must be "+" or "-".')
    if len(y[0]) > 4:
        return('Error: Numbers cannot be more than four digits.')
    if not x[0].isdigit() or not x[2].isdigit():
        return('Error: Numbers must only contain digits.')
    if len(x[0]) > len(x[2]):
        largest = len(x[0])
    if len(x[0]) < len(x[2]):
        largest = len(x[2])
    if len(x[0]) == len(x[2]):
        largest = len(x[0])
    dashes_len = (largest + 2)*'-'    
    if x[1] == '+': sums = str(int(x[0]) + int(x[2]))
    if x[1] == '-': sums = str(int(x[0]) - int(x[2]))

    first_num.append(str((6-int(len(x[0])))*' ')+ x[0])
    second_num.append((6-len(dashes_len))*' ' + x[1] + (len(dashes_len) - (len(x[2])+1))*' ' + x[2])
    dashes.append((6 - len(dashes_len))*' ' + dashes_len)
    ans.append((6 - len(sums))*' ' + str(sums))

first_num = ('    '.join(first_num))
second_num = ('    '.join(second_num))
dashes = ('    '.join(dashes))
ans = ('    '.join(ans))

arranged_list = [first_num + '\n', second_num + '\n', dashes + '\n']
arranged_problems = ''.join(arranged_list)

if bvalue == True:
    arranged_problems += ans
return arranged_problems


Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

So I too am at approximately this same step as you but there are some things I see that I think are wrong.

  1. Your error codes are all followed by a break but I believe this is unnecessary as return will cause this to never happen.
  2. I’m not sure what your y=sorted() line is for specifically but I think that it only appears in 1 other place so this might be throwing an error or something Idk.
    Edit- Never mind, I see what you used it for.
  3. I believe at the very bottom where bvalue==True you are missing a \n + to not throw an error.

To answer your question about the test cases. I believe they are coming in as [one big string as a list]sometimes present sometimes not) making this what, a tuple?
I’m using it with arithmetic_arranger(args, solve=None): and it seems to be working.
The problem with mine here is that all my error codes work but I can’t tell why I’m missing the other 6/10 Tests.
This might be the reason somehow but it appears to work in PyCharm.
The specs say the numbers should be right aligned. Can someone say explicitly what that means? Should the lines all be right shifted 4 spaces from the left hand side?

Edit: I think it boils down to these to results.
’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
3 988 \n+ 855 + 40 \n----- ------ \n 858 1028
with the top being the expected and the bottom being my own.
I find it greatly confusing not knowing if it wants me to return(print()) or just return and whether it wants it to look right when I print like the examples provided or come out with the \ns having \n so they show up like in the line expected part of the tester. Also, the examples provided seem to have the top number shifted over a space and I’m not sure if we are supposed to do that either or if that is some kind of typo.

Edit: I ran all my lines thru .rstrip() and suddenly I’m only failing 2/10 so despite it looking the same this was causing a problem.

And if anybody knows how to untruncate with ‘-vv’ I would like to know how.

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