完备代数方程

时间:2016-09-07 15:01:12

标签: python-3.x equation algebra

我正在尝试构建一个基于用户输入绘制方程式的应用程序。等式将采用斜率截距形式:y = mx + bm为斜率,by intercept。但是,这在python中对我不起作用!

我试过了:

>>> x = 3
>>> 1/2x

并且返回了这个:

  File "<stdin>", line 1
    1/2x
       ^
SyntaxError: invalid syntax
>>>

我如何才能使它返回1.5?

1 个答案:

答案 0 :(得分:0)

import re
mxb = re.compile('''(?P<slope>[(\d)/.]*)x\s+(?P<sign>[+-])\s+(?P<intercept>[\d./]*)''')
s = '1/2x + 5' 
match = mxb.match(s)
if match : 
    print( match.groupdict() )

在这里测试python正则表达式:pythex.org