Python Project - Arithmetic Formatter Help

Hi
I have almost completed the project but unable to resolve the last two errors. My output is according to given instructions but still getting error for test_arrangement and test_solutions
It be great if someone could point out my mistake. I am inserting my code below.

Thank You

def arithmetic_arranger(problems,result=False):
    lst = []
    arranged_problems = ""
    if((len(problems) > 5)):
        arranged_problems = "Error: Too many problems."
        
    else:
        for i in problems:
            lst.append(i.split())
            if(not(i.split()[0].isnumeric()) or not(i.split()[2].isnumeric())):
                return "Error: Numbers must only contain digits."
        for i in lst:
            if(len(i[0]) > 4):
                return "Error: Numbers cannot be more than four digits."
            if(not(len(i[0]) == 4)):
                spc = ''
                spc_up = len(i[0])
                spc_down = len(i[2])
                if spc_up < spc_down: 
                    spc_len = spc_down - spc_up 
                else:
                    spc_len = 0
            else:
                spc_len = 0
            spc = (spc_len + 2) * " "
            arranged_problems += spc + i[0] + "    "

        arranged_problems += "\n"

        for i in lst:
            if(len(i[2]) > 4):
                return "Error: Numbers cannot be more than four digits."
            if(i[1] == '*' or i[1] == '/'):
                return "Error: Operator must be '+' or '-'."

            if(not(len(i[0]) == 4)):
                spc = ''
                spc_up = len(i[0])
                spc_down = len(i[2])

                if(spc_up > spc_down):
                    spc_len = spc_up - spc_down
                else:
                    spc_len = 0
            else:
                spc_len = 4 - len(i[2])
            
            spc = (spc_len) * " "
            arranged_problems += i[1] + " " + spc + i[2] + "    "
            

        arranged_problems += "\n"

        for i in lst:
            spc_len = max(len(i[0]),len(i[2]))
            spc = (spc_len + 2) * "-"
            arranged_problems += spc + "    "

        arranged_problems += "\n"

        if(result == True):
            for i in lst:
                ev = str(i[0] + " " + i[1] + " " + i[2])
                spc_max = max(len(i[0]),len(i[2]))
                spc_len = 0
                if(eval(ev) < 0):
                    spc_len -= 1
                if(len(str(eval(ev))) < spc_max):
                    spc_len += spc_max - len(str(eval(ev)))
                spc = (spc_len + 2) * " "
                arranged_problems += spc + str(eval(ev)) + "    "

    return arranged_problems

I’m just appending the exercise’s link for anyone else…

Unrelated, and I’m not 100% sure, but I believe you can write

if not (...) just once

I tried to use it both ways before posting here. I think these errors may be related good programming practices rather than the programming itself.

Thanks for the advice anyway.

can you give the link to your repl?

What do the errors say

Link to me repo : link
Sorry for the late reply.

checkout out my repl for the errors.

I have changed your spaces to be dots so it is more visible:

....3......3801......45......123....
+.855....-....2....+.43....+..49....
-----....------....----....-----....

you have extra spaces at the end of each line

1 Like

you only throw error when operator is ‘/’ or ‘*/ what happen when you introduce ‘%’, you should write first the errors.
if op != ‘+’ or op !=’-’:
return error. for exameplo. and do this type of code for every error. so last you can solve the results better. divide and conquer

image.png

you can see and error which says your code accept letter like 3g5. i solved it with regular expresions
but you can use different ways. for example:
if n not in digits:
return false etc…

he can use str.rstrip()

1 Like

Oh I see, thank you for pointing that out.
Seems that was the problem after all thanks a lot.

Thank you for the suggestion. You guys are amazing.