在Python代码中查找多位数整数

2024-06-28 19:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

快速问题(使用python3.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()

这给我打印了一张所有整数的列表,但我不知道如何将整数保持在一起。有什么想法吗?在


Tags: thetotextininputif整数int