快速问题(使用 Python 3.x)- 我正在编写一个 Python 程序,它接受多行输入,然后搜索该输入,找到所有整数,将它们相加,然后输出结果。对于搜索和查找多位整数的最有效方法,我有点困惑——如果一行包含 12,我想找到 12 而不是 [1,2]。这是我的代码,未完成:
def tally():
#the first lines here are just to take multiple input
text = []
stripped_int = []
stopkey = "END"
while True:
nextline = input("Input line, END to quit>")
if nextline.strip() == stopkey:
break
text.append(nextline)
#now we get into the processing
#first, strip all non-digit characters
for i in text:
for x in i:
if x.lower() in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!',',','?']:
pass
else:
stripped_int.append(x)
print(stripped_int)
tally()
这会打印出所有整数的列表,但我对如何将整数保持在一起感到困惑。有任何想法吗?