python专用
1. 读取输入
1.1 方法1,sys.stdin
读取后是str,记得转换下自己需要的格式,下例中转换为int了
import sys
for lines in sys.stdin: # 读取每行
tem = lines.split(' ') # 取决于如何分割输入,这里是空格
# 如要求输出和
sums = 0
for i in tem:
sums += int(i) # 记得转换格式
print(sums)
方法2,input()
应用map函数读取整行,采用while true 可忽略多行的影响
这里,try except必不可少
依旧如上例,输入两个数,计算输出和
try:
while 1: # 或者 while True:
a,b = list(map(int,input().split(' ')))
print(a+b)
except:
pass
or
a=[]
while 1:
try:
a.append(list(map(int,input().strip().split())))
# 事实上,a是个list,a中每一项都记录了每一行的输入
# input读取的是某一行的所有输入
# strip()用于移除输入前后的空格或换行符
# split用来切分
except :
break # 这里很重要,pass是无法通过的
for i in a: # 这里其实i就是每一行的输入
print(i[0]+i[1])
or
try:
while 1:
a = [int(i) for i in input().split()]
print(sum(a))
except:
pass