Python-尝试将列表中的项目相乘

Den*_*ise 4 python python-3.x python-3.4

所以基本上我在这里要做的是询问用户输入随机字符串,例如:

asdf34fh2
Run Code Online (Sandbox Code Playgroud)

而且我想把它们中的数字拉到一个列表中然后得到[3,4,2]但是我一直在接受[34, 2].

import re 

def digit_product():        
    str1 = input("Enter a string with numbers: ")

    if str1.isalpha():
        print('Not a string with numbers')
        str1 = input("Enter a string with numbers: ")
    else:
        print(re.findall(r'\d+', str1))   

digit_product()      
Run Code Online (Sandbox Code Playgroud)

然后我想拿出那个数字列表并乘以它们,最终得到24.

pto*_*ato 7

你的正则表达式\d+是这里的罪魁祸首.它+意味着它匹配一个或多个连续数字(\d):

asdf34fh2
    ^-  ^
    \   \_ second match: one or more digits ("2")
     \____ first match: one or more digits ("34")
Run Code Online (Sandbox Code Playgroud)

看起来你只想匹配一个数字,所以\d没有使用+.