Search code examples
pythonloopsfor-loopdoubleformatter

For loop double the result just for a certain item in an array


So I use a for loop but somehow it loops 2 times for the first and the last item in the array. I tried and it apparently just happens when the len_a bigger than the rest (len_b and len_c)

So here's my code:

def arithmetic_arranger(problems):
    top = ""
    middle = ""
    lines = ""
    bottom = ""
    for problem in problems:
        #split em up
        nums = problem.split(" ")
        first_num = nums[0]
        symbol = nums[1]
        last_num = nums[2]
        if len(first_num) > 4 < len(last_num):
            return "gk boleh lebih dr 4 yhhhh"
        
        #find the total
        if symbol == "+":
            total = str(int(first_num) + int(last_num))
        elif symbol == "-":
            total = str(int(first_num) - int(last_num))
        else:
            return "cuma boleh + or - tolol"
        
        #find the distances for the top line
        len_a = len(first_num)
        len_b = len(last_num)
        len_c = len(total)
        maxi = max(len_a, len_b, len_c)
        lines = lines + "--" +"-" * maxi + "     "
        if maxi == len_a:
            top = top + " "*2 + first_num + "     "
            middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + "     "
            bottom = bottom + "  " + " " * (len_a - len_c) + total + "     "
        if maxi == len_b:
            top = top + " "*2 + " " * (len_b - len_a) + first_num + "     "
            middle = middle + symbol + " " + last_num + "     "
            bottom = bottom + "  " + " " * (len_b - len_c) + total + "     "
        else:
            top = top + " "*2 + " " * (len_c - len_a) + first_num + "     "
            middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + "     "
            bottom = bottom + "  " + total + "     "
    
    #assemble it
    joint = top, middle, lines, bottom
    answer = '\n'.join(joint)
    return answer
        
        

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

This is the output that i got

the output

The output that i expect to come is like this

expected output

It only doubles the first and the last problem from the array and i don't know which part that's wrong

ps. sorry for the shitty ass lines of code I'm pretty new at this thing TT


Solution

  • The problem is here:

    if maxi == len_a:
        top = top + " "*2 + first_num + "     "
        middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + "     "
        bottom = bottom + "  " + " " * (len_a - len_c) + total + "     "
    if maxi == len_b:
        top = top + " "*2 + " " * (len_b - len_a) + first_num + "     "
        middle = middle + symbol + " " + last_num + "     "
        bottom = bottom + "  " + " " * (len_b - len_c) + total + "     "
    else:
        top = top + " "*2 + " " * (len_c - len_a) + first_num + "     "
        middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + "     "
        bottom = bottom + "  " + total + "     "
    
    

    If maxi == len_a, the first if body will be executed, appending the result. Then the second if condition will be evaluated, and if true, the if body will be executed, otherwise the else body will be executed. Either way, the result is appended a second time. Only one of those three cases should be executed on a given loop iteration. You can fix it by changing the second if to an elif:

    if maxi == len_a:
        top = top + " "*2 + first_num + "     "
        middle = middle + symbol + " " + " " * (len_a - len_b) + last_num + "     "
        bottom = bottom + "  " + " " * (len_a - len_c) + total + "     "
    elif maxi == len_b:
        top = top + " "*2 + " " * (len_b - len_a) + first_num + "     "
        middle = middle + symbol + " " + last_num + "     "
        bottom = bottom + "  " + " " * (len_b - len_c) + total + "     "
    else:
        top = top + " "*2 + " " * (len_c - len_a) + first_num + "     "
        middle = middle + symbol + " " + " " * (len_c - len_b) + last_num + "     "
        bottom = bottom + "  " + total + "     "
    

    That way, only one case will be executed per loop iteration.