Python输入多行多组数据两个两求和

while True:
    try:
        a,b = input().split()
        a = int(a)
        b = int(b)
        print(a+b)
    except:
        break

方法二:

import sys
while True:
    line = sys.stdin.readline()
    if not line:
        break
    a, b = (int(x) for x in line.split())
    print(a + b)

运行:

输入1 2

       3 4

       5 6

结果  3

         7

        11

限定行数,多行求和:

from numpy import *

Q = int(input())   # 先输入一个数字代表需要输入几行,比如Q=4,那么就需要再输入4行数据
for i in range(Q):
    s = [int(n) for n in input().split()]
    print(sum(s))

结果:

2
2 3
5
3 4
7

你可能感兴趣的:(Python,Demo)