如何编辑根据输入分别显示结果的代码并避免重复

how can I edit my code that has shown results separately base on inputs and avoid duplicating

def aplusb(a, b):
    return a+b
    # write your code here

q=0
max=int(raw_input())
while q<max :
    a,b = map(int, raw_input().split())
    q=q+1
# q=aplusb(a,b)*q

for q in range(max):
    q = aplusb(a, b)
    print q


if __name__ == "__main__":
    q = int(raw_input())
    for i in range(q):
        inps = [int(_) for _ in raw_input().split()]
        print aplusb(inps[0], inps[1])

当我输入两个不同系列的数字时,如 (2,1)(3,6) 我希望得到这样的结果 (3) \N (9) 但他们现在显示 (9) /n (9)!我该如何解决?

if __name__...之前的代码读取第一个 while 循环中的所有输入,但随后只使用最后一个 a & b第二个 while 循环的迭代。

您可以在阅读时打印每对的结果,或者您必须保存所有输入以便第二个循环可以返回它。

问题是您的代码在打印出对它们的任何计算结果之前更改了 ab 的值。

我认为您不希望输入与输出交错。因此,您必须将输入或结果临时存储在列表中。这种方法存储输入,因为它更接近您的原始代码。

def aplusb(a, b):
    return a+b

inps = []
max=int(raw_input())

for q in range(max):
    inps.append(map(int, raw_input().split()))

# At this point, using your sample data, inps looks like [[2,1], [3,6]]

for a, b in inps:
    q = aplusb(a, b)
    print q

附带说明一下,如果您刚刚开始 Python,您 真的不应该 自学 Python 2. 请考虑安装和使用 Python 3 代替。