Scientific Computing with Python Projects - Arithmetic Formatter Format Is Wrong?

Tell us what’s happening:
I have done everything as told in the problem description and i am getting the correct answers as and the console shows my print statements use the proper format but i still get the e errors
FAIL: test_arrangement (test_module.UnitTests)
FAIL: test_solutions (test_module.UnitTests)

Your code so far

def arithmetic_arranger(problems,answer=False):
    import re
    split_probs=[]
    i=0
    numbers=[]
    operands=[]
    answers=[]
    
    for problem in problems:
        split_probs.append(problem.split())
      
        #check if too many problems 
    if len(problems)>5:
        prob1="Error: Too many problems."
        print(prob1)
        cond1=False
        return prob1
    else:
        cond1=True
        #check no multiple or division
    for problem in problems:
        if re.search('(/|\*)',problem):
                prob2="Error: Operator must be '+' or '-'."
                print(prob2)
                cond2=False
                return prob2
        else:
            cond2=True
    
    #make sure only digits
    cond3=True
    for problem in split_probs:
        
        i=0
    
        if cond3==True:
            while i<3:
                if problem[i].isnumeric() or problem[i]=='+' or problem[i]=='-':
                    cond3=True
                    i=i+1
                    
                else:
                    cond3=False
                    prob3="Error: Numbers must only contain digits."
                    print(prob3)
                    return prob3
        else:
            break
    #check digits less than 5
    cond4=True
    i=0
    for problem in split_probs:
        if cond4==True:
            i=0
            while i<3:
                if len(problem[i])>4:
                    prob4="Error: Numbers cannot be more than four digits."
                    print(prob4)
                    cond4=False
                    return prob4
                    
                else:
                    
                    cond4=True   
                    i=i+1
        else:
            break
    if (cond1 and cond2 and cond3 and cond4)==True:
        if answer==True:
            countnum=0
            countop=0
            
            for inp in split_probs:
               i=0
               while i<3: 
                    if inp[i].isnumeric():
                        numbers.append(inp[i])
                        i=i+1
                    else:
                        operands.append(inp[i])
                        i=i+1
                        
            while countop<len(operands):
                    num1=numbers[countnum]
                    
                    countnum=countnum+1
                    
                    num2=numbers[countnum]
                    
                    
                    
                    if operands[countop]=='-':
                        
                        num3=int(num1)-int(num2)
                        answers.append(num3)
                        
                        countop=countop+1
                        countnum=countnum+1
                    else:
                        num3=int(num1)+int(num2)
                        answers.append(num3)
                        countop=countop+1
                        countnum=countnum+1
                    
        botlin=''  
        toplin=''
        offset=0
        thirdlin=''
        forthlin=''
        cnt=0
        for parts in split_probs:
            if len(parts[0])>(len(parts[2])):
                     offset=len(parts[0])-len(parts[2])-1
                     toplin=toplin+" {0}    ".format(parts[0])
                     botlin=botlin + "{0}".format(parts[1]) + " "*offset +" {0}    ".format(parts[2])
                     thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+1)+"    "
                     if answer==True:
                         answeroff=len(parts[0])+1-len(str(answers[cnt]))
                         forthlin=forthlin+" "*answeroff +"{0}    ".format(answers[cnt])
                         cnt=cnt+1
                    
            elif len(parts[0])<(len(parts[2])):
                    offset=len(parts[2])+2-len(parts[0])
                    toplin=toplin+ " "*offset + "{0}    ".format(parts[0])
                    botlin=botlin+"{0} {1}    ".format(parts[1],parts[2])
                    thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+2)+"    "
                    if answer==True:
                         answeroff=len(parts[2])+2-len(str(answers[cnt]))
                         forthlin=forthlin+" "*answeroff+"{0}    ".format(answers[cnt])
                         
                         cnt=cnt+1
                    
            else:
                    toplin=toplin+"  {0}    ".format(parts[0])
                    
                    botlin=botlin + "{0} {1}    ".format(parts[1],parts[2])
                    thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+2)+"    "     
                    if answer==True:
                         answeroff=len(parts[0])+2-len(str(answers[cnt]))
                         forthlin=forthlin+" "*answeroff+ "{0}    ".format(answers[cnt])
                         cnt=cnt+1
                    
            
        
        
               
                    
                    
                            
                
        print(toplin)
        print(botlin)
        print(thirdlin)
        print(forthlin)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 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 (’).

I’m pretty sure there is more text than that, what else do the errors say?

1 Like

Does this provide any helpful information? Is it because I hard coded the indentations between the problems?

It looks like you are printing the result instead of returning it

it says AssertionError: None != "..."

your output is None when it expects a string

1 Like

At the end of your code you print the lines. You are not supposed to print them, you are supposed to return a string, something like this:

return topline+"\n"+botline+"\n"+....

I can’t believe I didn’t notice I wasn’t returning anything, thanks for pointing that out. But it still doesn’t seem to be returning the right format when i return the strings

It looks like you have extra space at the end of each line, and an extra _ in one case.

I fixed most of the issues but it seems like I have an extra line but when i remove it, it gives me an error im missing a \n?

vs when i take out the \n between third and fourth line and it tells me i am missing the \n??

Perhaps you should only have an extra \n when the 4th line is present? The solutions to the arithmetic problems shouldn’t always be shown.

I am so dumb I forgot about the condition to only return the answers if passed true. Thank you so much I was banging my head for hours.

We’ve all been there with bugs that are obvious only in hindsight