Arithmetic arranger problem debug

Hi all, I am working on a arithmetic arranger problem. I have tested it myself in Visualize Python and it works. However, it could only pass 7 out of 10 test cases. The expected output looks the same as mine.

def arithmetic_arranger(sentences, boolean=False):
    if len(sentences)>5:
        return "Error: Too many problems."
    output1 =""
    output2 = ""
    output3 = ""
    line = ""
    for item in sentences:
        sign=str()
        
        if item.count('+')==1:
            sign='+'
            cal=item.split('+')
            if (cal[0].strip()).isdigit()==False:
                return "Error: Numbers must only contain digits."
            elif (cal[1].strip()).isdigit()==False:
                return "Error: Numbers must only contain digits."
            else:
                res=int(cal[0])+int(cal[1])
        elif item.count('-')==1:
            sign='-'
            cal=item.split('-')
            if (cal[0].strip()).isdigit()==False:
                return "Error: Numbers must only contain digits."
            elif (cal[1].strip()).isdigit()==False:
                return "Error: Numbers must only contain digits."
            else:
                res=int(cal[0])-int(cal[1])
        else:
            return "Error: Operator must be '+' or '-'."
        length = max(len(cal[0].strip()),len(cal[1].strip()))+2
        if length-2 > 4:
            return "Error: Numbers cannot be more than four digits."
        else: 
            pos1=length - len(cal[0].strip())
            output1 +=" "*(pos1) +cal[0].strip()+"    "
            pos2=length - len(cal[1].strip())
            output2 +=sign + " "*(pos2-1)+cal[1].strip()+"    "
            line += "-"*length+"    "
    if not boolean:
        return (output1[:-4]+"\n"+output2[:-4]+"\n"+line[:-4])
    else:     
        return (output1[:-4]+"\n"+output2[:-4]+"\n"+line[:-4]+"\n"+output3[:-4])

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

I’ve edited your post 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 (’).

You should not be printing anything.

1 Like

Thank you! I have updated my code. It passed 7 out of 10. There might be some problem with the boolean part. I’m not sure. Please advise.

What errors are you getting? Usually by the failing test there’s some information what to look at closer.

1 Like