Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

When i print out the final answer its the same as what it should be but when I run the tests all the tests are wrong

Your code so far

numbers=[]
def errors(list1, show_answers):
    for problem in list1:
        space= problem.index(" ")
        centre1 = problem[0:space]
        if len(centre1) > 4:
            print('Error: Numbers cannot be more than four digits.')
            
        centre2 = problem[space + 2:]
        if len(centre2) > 5:
            print('Error: Numbers cannot be more than four digits.')
        
        if any(c.isalpha() == True for c in problem):
            print('Error: Numbers must only contain digits.') 
        if "/" in problem:
            print("Error: Operator must be '+' or '-'.") 
        elif "*" in problem:
            print("Error: Operator must be '+' or '-'.") 
        
    if len(numbers) > 5:
        print('Error: Too many problems.')
    new_list(list1, show_answers)


def arithmetic_arranger(problems, show_answers=False):
    
    errors(problems, show_answers)
def new_list(list1, show_answers):
    #print(list1)
    for problem in list1:  
        if "+" in problem:
            addindex = problem.index("+")
            
            number1 = problem[0:addindex - 1] +"+"
            number2 = problem[addindex + 1:] 
            
        if "-" in problem:
            minusindex = problem.index("-")
            number1 = problem[:minusindex - 1] + "-"
            number2 = problem[minusindex + 1:]      
        numbers.append(number1.replace(" ","") +number2.replace(" ","")) 
    #print(numbers)     
    flormat(numbers, show_answers)
def flormat(sums, show_answers):
    finalstring1=[]
    finalstring2=[]
    finalstring3=[]
    finalstring4=[]
    for digits in sums:
        if "+" in digits:
            number1 = digits[0:digits.index("+")]
            number2 = digits[digits.index("+") + 1:]
            finalnumber=str(int(number1) + int(number2))
            maxdig = max(len(number1), len(number2), ) + 2
            while len(number1) < maxdig :
                number1 = " " + number1
            finalstring1.append(number1)  
             
            while len(number2)  < maxdig - 1 :
                number2 = " " + number2 
            number2 = "+" + number2            
            finalstring2.append(number2) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber   
                 
            
            #print(number2)
            hyphens= (max(len(number1.strip(" ")),len(number2.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens    
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)    
            #print(finalstring3)
            
            
        if "-" in digits:
            number1m = digits[0:digits.index("-")]
            number2m = digits[digits.index("-") +1:]
            finalnumber=str(int(number1m) - int(number2m))
            maxdig = max(len(number1m), len(number2m)) + 2
            while len(number1m) < maxdig :
                number1m = " " + number1m
            finalstring1.append(number1m) 
             
            while len(number2m) < maxdig - 1 :
                number2m = " " + number2m
            number2m = "-" + number2m                
            finalstring2.append(number2m) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber  
                #print(finalnumber)  
            hyphens= (max(len(number1m.strip(" ")),len(number2m.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2m):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1m):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens        
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)     
        #print(finalstring4)
    x= "    ".join(finalstring1)        
    y= "    ".join(finalstring2)
    z= "    ".join(finalstring3)
    a= "    ".join(finalstring4)
    if show_answers == False:
        return (x + "\n" + y + "\n" + z + "\n")
    elif show_answers == True:
        return (x + "\n" + y + "\n" + z + "\n" + a)
    
    
        

            
            

#arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)


#print(f'\n{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/130.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

you need to return not print


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

Thanks for replying but ive already tried returning it and its not working

Are you still failing all the tests?

Please post a good example of your input and output.

here you are printing not returning

Its still failing after ive changed it

yes its still failing .

image

open the browser console with F12 to see a more verbose output of the tests.

Use repr() to call your function so you can see raw string output and compare with the tests.

print(repr(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"])))

when i use that code it prints None but i dont get why

Please share your updated code

numbers=[]
def errors(list1, show_answers):
    for problem in list1:
        space= problem.index(" ")
        centre1 = problem[0:space]
        if len(centre1) > 4:
            return('Error: Numbers cannot be more than four digits.')
            
        centre2 = problem[space + 2:]
        if len(centre2) > 5:
            return('Error: Numbers cannot be more than four digits.')
        
        if any(c.isalpha() == True for c in problem):
            return('Error: Numbers must only contain digits.') 
        if "/" in problem:
            return("Error: Operator must be '+' or '-'.") 
        elif "*" in problem:
            return("Error: Operator must be '+' or '-'.") 
        
    if len(numbers) > 5:
        return('Error: Too many problems.')
    new_list(list1, show_answers)


def arithmetic_arranger(problems, show_answers=False):
    
    errors(problems, show_answers)
def new_list(list1, show_answers):
    #print(list1)
    for problem in list1:  
        if "+" in problem:
            addindex = problem.index("+")
            
            number1 = problem[0:addindex - 1] +"+"
            number2 = problem[addindex + 1:] 
            
        if "-" in problem:
            minusindex = problem.index("-")
            number1 = problem[:minusindex - 1] + "-"
            number2 = problem[minusindex + 1:]      
        numbers.append(number1.replace(" ","") +number2.replace(" ","")) 
    #print(numbers)     
    flormat(numbers, show_answers)
def flormat(sums, show_answers):
    finalstring1=[]
    finalstring2=[]
    finalstring3=[]
    finalstring4=[]
    for digits in sums:
        if "+" in digits:
            number1 = digits[0:digits.index("+")]
            number2 = digits[digits.index("+") + 1:]
            finalnumber=str(int(number1) + int(number2))
            maxdig = max(len(number1), len(number2), ) + 2
            while len(number1) < maxdig :
                number1 = " " + number1
            finalstring1.append(number1)  
             
            while len(number2)  < maxdig - 1 :
                number2 = " " + number2 
            number2 = "+" + number2            
            finalstring2.append(number2) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber   
                 
            
            #print(number2)
            hyphens= (max(len(number1.strip(" ")),len(number2.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens    
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)    
            #print(finalstring3)
            
            
        if "-" in digits:
            number1m = digits[0:digits.index("-")]
            number2m = digits[digits.index("-") +1:]
            finalnumber=str(int(number1m) - int(number2m))
            maxdig = max(len(number1m), len(number2m)) + 2
            while len(number1m) < maxdig :
                number1m = " " + number1m
            finalstring1.append(number1m) 
             
            while len(number2m) < maxdig - 1 :
                number2m = " " + number2m
            number2m = "-" + number2m                
            finalstring2.append(number2m) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber  
                #print(finalnumber)  
            hyphens= (max(len(number1m.strip(" ")),len(number2m.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2m):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1m):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens        
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)     
        #print(finalstring4)
    x= "    ".join(finalstring1)        
    y= "    ".join(finalstring2)
    z= "    ".join(finalstring3)
    a= "    ".join(finalstring4)
    if show_answers == False:
        return (x + "\n" + y + "\n" + z + "\n")
    elif show_answers == True:
        return (x + "\n" + y + "\n" + z + "\n" + a)

where are you using the print statement? I don’t see it

that was when i was testing to see why it doesnt work i replaced the last return with a print to see what it returns

you shouldn’t, instead print the result of the function call

this is your function, it doesn’t have a return statement, you should always return the output

I returned it at the end

That is not the end of arithmetic_formatter, is it?

i see what i did wrong ive changed it ill send the new code

i changed this code but its still not working

numbers=[]
def errors(list1, show_answers):
    for problem in list1:
        space= problem.index(" ")
        centre1 = problem[0:space]
        if len(centre1) > 4:
            return('Error: Numbers cannot be more than four digits.')
            
        centre2 = problem[space + 2:]
        if len(centre2) > 5:
            return('Error: Numbers cannot be more than four digits.')
        
        if any(c.isalpha() == True for c in problem):
            return('Error: Numbers must only contain digits.') 
        if "/" in problem:
            return("Error: Operator must be '+' or '-'.") 
        elif "*" in problem:
            return("Error: Operator must be '+' or '-'.") 
        
    if len(numbers) > 5:
        return('Error: Too many problems.')
    return(new_list(list1, show_answers))


def arithmetic_arranger(problems, show_answers=False):
    
    return(errors(problems, show_answers))
def new_list(list1, show_answers):
    #print(list1)
    for problem in list1:  
        if "+" in problem:
            addindex = problem.index("+")
            
            number1 = problem[0:addindex - 1] +"+"
            number2 = problem[addindex + 1:] 
            
        if "-" in problem:
            minusindex = problem.index("-")
            number1 = problem[:minusindex - 1] + "-"
            number2 = problem[minusindex + 1:]      
        numbers.append(number1.replace(" ","") +number2.replace(" ","")) 
    #print(numbers)     
    return(flormat(numbers, show_answers))
def flormat(sums, show_answers):
    finalstring1=[]
    finalstring2=[]
    finalstring3=[]
    finalstring4=[]
    for digits in sums:
        if "+" in digits:
            number1 = digits[0:digits.index("+")]
            number2 = digits[digits.index("+") + 1:]
            finalnumber=str(int(number1) + int(number2))
            maxdig = max(len(number1), len(number2), ) + 2
            while len(number1) < maxdig :
                number1 = " " + number1
            finalstring1.append(number1)  
             
            while len(number2)  < maxdig - 1 :
                number2 = " " + number2 
            number2 = "+" + number2            
            finalstring2.append(number2) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber   
                 
            
            #print(number2)
            hyphens= (max(len(number1.strip(" ")),len(number2.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens    
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)    
            #print(finalstring3)
            
            
        if "-" in digits:
            number1m = digits[0:digits.index("-")]
            number2m = digits[digits.index("-") +1:]
            finalnumber=str(int(number1m) - int(number2m))
            maxdig = max(len(number1m), len(number2m)) + 2
            while len(number1m) < maxdig :
                number1m = " " + number1m
            finalstring1.append(number1m) 
             
            while len(number2m) < maxdig - 1 :
                number2m = " " + number2m
            number2m = "-" + number2m                
            finalstring2.append(number2m) 
            while len(finalnumber) < maxdig :
                finalnumber = " " + finalnumber  
                #print(finalnumber)  
            hyphens= (max(len(number1m.strip(" ")),len(number2m.strip(" ")),len(finalnumber.strip(" "))) * "-" )
            while len(hyphens) < len(number2m):
                hyphens = " " + hyphens
            while len(hyphens) < len(number1m):
                hyphens = " " + hyphens    
            while len(hyphens) < len(finalnumber):
                hyphens = " " + hyphens        
            finalstring3.append(hyphens)    
            finalstring4.append(finalnumber)     
        #print(finalstring4)
    x= "    ".join(finalstring1)        
    y= "    ".join(finalstring2)
    z= "    ".join(finalstring3)
    a= "    ".join(finalstring4)
    if show_answers == False:
        return (x + "\n" + y + "\n" + z + "\n")
    elif show_answers == True:
        return (x + "\n" + y + "\n" + z + "\n" + a)