Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
Hello , My name Is Shu…
it would be nice if some one can show me why does the code doesn’t match the test package

def arithmetic_arranger(problems, result = True):
    import re
    
    import math
    import sys
    data = []
    c =[]
    n = 0
    long = 0
    
    sum = []
    res = 0
    
    try:
        for problem in problems:
           
            problem=problem.split()
            
            #problem[0] and problem [2] is the integer
            #problem[1] is operator
            if re.findall("\D+", problem[0]) :
                sys.exit('Error: Numbers must only contain digits')
            if re.findall("\D+", problem[2]) :
                sys.exit('Error: Numbers must only contain digits') 
            
                
            if re.findall("\d{5,}", problem[0]) : 
                sys.exit ('Error: Numbers cannot be more than four digits.')               
            if re.findall("\d{5,}", problem[2]) : 
                sys.exit ('Error: Numbers cannot be more than four digits.')
            
            if problem[1] in ['+','-']:
                if problem[1] == "+" :
                    res = int(problem[0]) + int(problem [2])
                    
                    sum.append(res)
                if problem[1] == "-":
                    
                    res = int(problem[0]) - int(problem [2])
                    sum.append(res)
            else :  
                sys.exit ("Error: Operator must be '+' or '-'.")
            if len(problem) == 3 :
                data.append(problem)
        #find len
                if len(problem[0])> long :
                    long = len(problem[0])
                if len(problem[2]) > long:
                    long = len(problem[2])
                c.append(long)
            problem[0]
                
    except : pass

    
    def arranged_problems():
        try :
            
            n = 0
            n = len(data)
            for i in range(n-1):
                print (" ",('{:>%d}'%c[i]).format(data[i][0]), end = '    ')
            print (" ",('{:>%d}'%c[-1]).format(data[-1][0]), end = '\n')
            for i in range(n-1):
                print (data[i][1],('{:>%d}'%c[i]).format(data[i][2]), end ='    ')
            print (" ",('{:>%d}'%c[-1]).format(data[-1][2]), end = '\n')       
            for i in range(n-1):
                print ("-"*(2+c[i]), end= "    ")
            print ("-"*(2+c[-1]), end = '\n')
        
            
        except: print("failed")
        if result == True:
            for i in range(len(sum)-1):
                print(" ",('{:>%d}'%c[i]).format(sum[i]), end="    ")
            print (" ",('{:>%d}'%c[-1]).format(sum[-1]), end = '\n')  
        return

    return arranged_problems()

this is my code so far
i am using chrome


this is the error

any help would be appreciated thank you
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

okay i will try to fix it

I fixed some spacing issue
I updated my code to this :

def arithmetic_arranger(problems, result = False):
    import re
    
    import math
    import sys
    data = []
    c =[]
    
    long = 0
    
    sum = []
    res = 0
    
    try:
        for problem in problems:
           
            problem=problem.split()
            
            #problem[0] and problem [2] is the integer
            #problem[1] is operator
            if re.findall("\D+", problem[0]) :
                sys.exit('Error: Numbers must only contain digits')
            if re.findall("\D+", problem[2]) :
                sys.exit('Error: Numbers must only contain digits') 
            
                
            if re.findall("\d{5,}", problem[0]) : 
                sys.exit ('Error: Numbers cannot be more than four digits.')               
            if re.findall("\d{5,}", problem[2]) : 
                sys.exit ('Error: Numbers cannot be more than four digits.')
            
            if problem[1] in ['+','-']:
                if problem[1] == "+" :
                    res = int(problem[0]) + int(problem [2])
                    
                    sum.append(res)
                if problem[1] == "-":
                    
                    res = int(problem[0]) - int(problem [2])
                    sum.append(res)
            else :  
                sys.exit ("Error: Operator must be '+' or '-'.")
            if len(problem) == 3 :
                data.append(problem)
        #find len
                if len(problem[0])> long :
                    long = len(problem[0])
                if len(problem[2]) > long:
                    long = len(problem[2])
                c.append(long)
            problem[0]
                
    except : pass

    
    def arranged_problems():
        try :
            spacing = []
            n = 0
            n = len(data)
            for i in range(n-1):
                print (" ",('{:>%d}'%c[i]).format(data[i][0]), end = '    ')
            print (" ",('{:>%d}'%c[-1]).format(data[-1][0]), end = '\n')
            for i in range(n-1):
                print (data[i][1],('{:>%d}'%c[i]).format(data[i][2]), end ='    ')
            print (data[-1][1],('{:>%d}'%c[-1]).format(data[-1][2]), end = '\n')       
            for i in range(n-1):
                spacing.append(2+c[i])
                print ("-"*(2+c[i]), end= "    ") 
            print ("-"*(2+c[-1]), end = '\n')

        except: pass   
        
        if result == True:
            for i in range(len(sum)-1):                    
                print(('{:>%d}'%spacing[i]).format(sum[i]), end="    ")
            print (('{:>%d}'%spacing[-1]).format(sum[-1]), end = '\n')
            
        return 

    return arranged_problems()


    

Isn’t that supposed to be return none, until second parameter set to True?

I don’t have idea what to put in the return to show the code as string what should i do ?

Thank you for your feedback!

thank you for your feed back ! I have passed the test!

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