Arithmetic formatter fails but output is identical

Hey I can’t seem to get this right.
The output looks Identical to me when I do a == test its True. But and “is” is False. Sorry I really don’t know what I’ve done wrong.

Please see code:

def arithmetic_arranger(problems):
arranged_problems = str()
# pulling equation list
if len(problems) <= 2:
sums = problems[0]
else:
sums = problems
# Too many problem error
if len(sums) > 5:
print(“Error: Too many problems.”)
exit()

l1 = str()
l2 = str()
l3 = str()
l4 = str()
f = 0
for x in sums:
    asp = 0
    a = 0
    alen = 0
    bsp = 0
    b = 0
    blen = 0
    c = 0
    asp = x.find(' ')
    a = x[0:asp]
    alen = len(a)
    bsp = x.find(' ', asp + 1)
    b = x[bsp + 1:len(x)]
    blen = len(b)
    c = x[asp + 1:bsp]

    if a.isdecimal() is not True:
        print("Error: Numbers must only contain digits.")
        exit()
    if b.isdecimal() is not True:
        print("Error: Numbers must only contain digits.")
        exit()

    if c == '+':
        d = int(int(a) + int(b))
    elif c == '-':
        d = int(int(a) - int(b))
    else:
        print("Error: Operator must be '+' or '-'.")
        exit()

    if max(alen, blen) > 4:
        print("Error: Numbers cannot be more than four digits.")
        exit()

    dlen = len(str(d))
    d = str(d)
    dep = max(alen + 2, blen + 2, dlen + 1)
    e = str("-" * dep)
    if f < 1:
        l1 = l1 + a.rjust(dep, " ")
        l2 = l2 + c + b.rjust(dep - 1, " ")
        l3 = l3 + e.rjust(dep, " ")
        l4 = l4 + d.rjust(dep, " ")
        f = f + 1
    else:
        l1 = l1 + a.rjust(4 + dep, " ")
        l2 = l2 + c.rjust(5," ") + b.rjust(dep - 1, " ")
        l3 = l3 + e.rjust(4 + dep, " ")
        l4 = l4 + d.rjust(4 + dep, " ")

if True in problems:
    arranged_problems = l1 + '\n' + l2 + '\n' + l3 + '\n' + l4
else:
    arranged_problems = l1 + '\n' + l2 + '\n' + l3
return arranged_problems

For a start your code will fail several tests because you are supposed to return error messages - not print them and call exit().
Then it would be nice if you at least posted the errors you are getting, ideally even the link to your replit.

Okay thanks I’ll fix that but it’s not even getting past the second test.

Okay thanks I’ll fix it.
it would be:
arranged_problems = (“Error: Too many problems.”)
return arranged_problems

is that correct?
replit

Yes.
The other issues come from the fact you are not taking in a second argument in the function.

Thanks I passed. I really appreciate it.

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