For 循环将数组中某个项目的结果加倍

问题描述 投票:0回答:1

所以我使用了 for 循环,但不知何故它对数组中的第一个和最后一个项目循环了 2 次。我尝试过,显然只有当 len_a 大于其余部分(len_b 和 len_c)时才会发生

这是我的代码:

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"]))

这是我得到的输出

the output

我期望的输出是这样的

expected output

它只将数组中的第一个和最后一个问题加倍,我不知道哪一部分是错误的

ps。抱歉,这些糟糕的代码行我对这件事还很陌生 TT

python loops for-loop double formatter
1个回答
0
投票

问题就在这里:

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 + "     "

如果

len_a
len_b
maxi
都相等,则
if
条件将为 true,从而导致重复输入。您可以通过将第二个
if
更改为
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 + "     "

这样,当它们都相等时,只有第一个

if
主体会被执行。

© www.soinside.com 2019 - 2024. All rights reserved.