Arithmetic Formatter: Executed in Jupyter but not getting executed in Repl.it

Hi, I am able to execute all the queries which are in test module in Jupyter notebook, however, when I copy pasted same code, I am getting few errors in Repl.it.

Please help me around.

Code below:

def arithmetic_arranger(data, condition = False):
    
    def too_many(data):
        global oper, sets, too_many_result
        oper = list()
        sets = list()
        too_many_result = []
        if len(data)>5:
            print('Error: Too many problems')
            too_many_result.append(False)
        else:
            too_many_result.append(True)
            for sets_b in data:
                sets_l = sets_b.split()
                oper.append(sets_l[1])
                sets.append(sets_l)
        return(oper, sets, too_many_result)
    
   
    def digit_len_checker(sets):
        global digit_len_checker_result
        digit_len_checker_result = []
        dl_set = sets
        digit = []
        length = []
        for dl in dl_set:
            if dl[0].isdigit() == True and dl[-1].isdigit() == True:
                digit.append(True)
            else:
                digit.append(False)

            if len(dl[0]) < 5 and len(dl[-1]) < 5:
                length.append(True)
            else:
                length.append(False)
        if False in digit:
            print("Error: Numbers must only contain digits.")
        if False in length:
            print("Error: Numbers cannot be more than four digits.")
        if False in digit or False in length:
            digit_len_checker_result.append(False)
        else:
            digit_len_checker_result.append(True)
        return(digit_len_checker_result)
 
      
    
    def operator_checker(oper):
        oper_condition = []
        for x in oper:
            if x == '+' or x =='-':
                oper_condition.append(True)
            else:
                oper_condition.append(False)
        if False in oper_condition:
            print("Error: Operator must be '+' or '-'.")
            return(False)
        else:
            return(True)
               
    
    
    def operation(x):
        global result
        result = []
        if x == True:
            for y in sets:
                if y[1] == '+':
                    calc = int(y[0]) + int(y[2])
                    result.append(calc)
                else:
                    calc = int(y[0]) - int(y[2])
                    result.append(calc)
        return(result)
    

    def printing(sets, result):
        global fr, sr, op, ln, re
        fr = []
        sr = []
        op = []
        ln = []
        re = result
        for x in sets:
            fr.append(x[0])
            sr.append(x[2])
            op.append(x[1])
            if len(x[0])>len(x[2]):
                ln.append(len(x[0]))
            else:
                ln.append(len(x[2]))
        if condition == True:
            if len(sets) == 4:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}{re[3]:>10}"
            elif len(sets) == 5:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}{fr[4]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}{op[4]:>{9-ln[4]}}{sr[4]:>{ln[4]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}{'-'*ln[4]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}{re[3]:>10}{re[4]:>10}"
            elif len(sets) == 3:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}"
            elif len(sets) == 2:
                output = f"{fr[0]:>10}{fr[1]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}\n{re[0]:>10}{re[1]:>10}"
            else:
                output = f"{fr[0]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}\n{'-'*ln[0]:>10}\n{re[0]:>10}"
        else:
            if len(sets) == 4:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}"
            elif len(sets) == 5:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}{fr[4]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}{op[4]:>{9-ln[4]}}{sr[4]:>{ln[4]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}{'-'*ln[4]:>10}"
            elif len(sets) == 3:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}"
            elif len(sets) == 2:
                output = f"{fr[0]:>10}{fr[1]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}"
            else:
                output = f"{fr[0]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}\n{'-'*ln[0]:>10}"
        return(output)
    
        
    
    # Function Calling
    too_many(data)
    if True in too_many_result:
        digit_len_checker(sets)
        if True in digit_len_checker_result:
            run_status = operator_checker(oper)
            if run_status == True:
                operation(run_status)
                arranged_problems = printing(sets, result)
                return(arranged_problems)

I have updated the code: Please refer the new one:

def arithmetic_arranger(data, condition = False):
    
    def too_many(data):
        global oper, sets, too_many_result
        oper = list()
        sets = list()
        too_many_result = []
        if len(data)>5:
            too_many_result.append(False)
        else:
            too_many_result.append(True)
            for sets_b in data:
                sets_l = sets_b.split()
                oper.append(sets_l[1])
                sets.append(sets_l)
        return(oper, sets, too_many_result)
    
   
    def digit_checker(sets):
        global digit_checker_result
        digit_checker_result = []
        digit = []
        dl_set = sets
        for dl in dl_set:
            if dl[0].isdigit() == True and dl[-1].isdigit() == True:
                digit.append(True)
            else:
                digit.append(False)
        if False in digit:
            digit_checker_result.append(False)
        else:
            digit_checker_result.append(True)
        return(digit_checker_result)
 
    def len_checker(sets):
        global length_checker_result
        dl_set = sets
        length_checker_result = []
        length = []
        for dl in dl_set:
            if len(dl[0]) < 5 and len(dl[-1]) < 5:
                length.append(True)
            else:
                length.append(False)
        if False in length:
            length_checker_result.append(False)
        else:
            length_checker_result.append(True)
        return(length_checker_result)
      
    
    def operator_checker(oper):
        oper_condition = []
        for x in oper:
            if x == '+' or x =='-':
                oper_condition.append(True)
            else:
                oper_condition.append(False)
        if False in oper_condition:
            return(False)
        else:
            return(True)
               
    
    
    def operation(x):
        global result
        result = []
        if x == True:
            for y in sets:
                if y[1] == '+':
                    calc = int(y[0]) + int(y[2])
                    result.append(calc)
                else:
                    calc = int(y[0]) - int(y[2])
                    result.append(calc)
        return(result)
    

    def printing(sets, result):
        global fr, sr, op, ln, re
        fr = []
        sr = []
        op = []
        ln = []
        re = result
        for x in sets:
            fr.append(x[0])
            sr.append(x[2])
            op.append(x[1])
            if len(x[0])>len(x[2]):
                ln.append(len(x[0]))
            else:
                ln.append(len(x[2]))
        if condition == True:
            if len(sets) == 4:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}{re[3]:>10}"
            elif len(sets) == 5:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}{fr[4]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}{op[4]:>{9-ln[4]}}{sr[4]:>{ln[4]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}{'-'*ln[4]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}{re[3]:>10}{re[4]:>10}"
            elif len(sets) == 3:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}\n{re[0]:>10}{re[1]:>10}{re[2]:>10}"
            elif len(sets) == 2:
                output = f"{fr[0]:>10}{fr[1]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}\n{re[0]:>10}{re[1]:>10}"
            else:
                output = f"{fr[0]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}\n{'-'*ln[0]:>10}\n{re[0]:>10}"
        else:
            if len(sets) == 4:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}"
            elif len(sets) == 5:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}{fr[3]:>10}{fr[4]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}{op[3]:>{9-ln[3]}}{sr[3]:>{ln[3]+1}}{op[4]:>{9-ln[4]}}{sr[4]:>{ln[4]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}{'-'*ln[3]:>10}{'-'*ln[4]:>10}"
            elif len(sets) == 3:
                output = f"{fr[0]:>10}{fr[1]:>10}{fr[2]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}{op[2]:>{9-ln[2]}}{sr[2]:>{ln[2]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}{'-'*ln[2]:>10}"
            elif len(sets) == 2:
                output = f"{fr[0]:>10}{fr[1]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}{op[1]:>{9-ln[1]}}{sr[1]:>{ln[1]+1}}\n{'-'*ln[0]:>10}{'-'*ln[1]:>10}"
            else:
                output = f"{fr[0]:>10}\n{op[0]:>{9-ln[0]}}{sr[0]:>{ln[0]+1}}\n{'-'*ln[0]:>10}"
        return(output)
    
        
    
    # Function Calling
    too_many(data)
    if True in too_many_result:
        digit_checker(sets)
        if True in digit_checker_result:
            len_checker(sets)
            if True in length_checker_result:
                run_status = operator_checker(oper)
                if run_status == True:
                    operation(run_status)
                    arranged_problems = printing(sets, result)
                    return(arranged_problems)
                else:
                    arranged_problems = "Error: Operator must be '+' or '-'."
                    return(arranged_problems)
            else:
                arranged_problems = "Error: Numbers cannot be more than four digits."
                return(arranged_problems)
        else:
            arranged_problems = "Error: Numbers must only contain digits."
            return(arranged_problems)
    else:
        arranged_problems = 'Error: Too many problems.'
        return(arranged_problems)

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 (’).

Thank you so much !

Can you please help me with the issue in the code.
I have also pasted the link for better understanding.

Hello Chrisrajt.

I could execute your code, It looks like there are two tests you are failing.

Ran 6 tests in 0.004s
FAILED (failures=2)

You have to look carefully at how you are printing the line that separates the result.

This your result with your code:
image

Have to be like this:
image

You have to be carefully folloing the number of spaces.
image

Thank you for your help!!

I was able to fix the issue and get the output.

Thank you again for your support :smile:

1 Like