i know im 3 years late, but you could use: numeric_const_pattern = '[+]? (?: (?: \d* . \d+ ) | (?: -?\d+ .? ) )(?: [xyXY(),] [+]? \d+ ) ?'
rx = re.compile(numeric_const_pattern, re.VERBOSE) num = rx.findall(problem)
a, b = num, num to find the values as I've found that simply throwing an accidental letter in there breaks the whole program. You could also modify numeric_const_pattern so it could be used to find numbers for the average. Finally, I wanted to add that you could use a switch statement instead of elifs for detecting the function. PS, you need to import re to use re.findall/rx.findall