更改输入位置以在python中调用函数

时间:2018-12-19 13:33:29

标签: python python-3.x python-import sympy cnf

这是我的代码:

from sympy import symbols, Equivalent
from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z


def eq(a, b):
    return fnc(Equivalent(a, b), True)

inr = str(input('Enter formula: '))
x = ' '
for i in inr:
     #print(inr[inr.index(i-2)])
     if i == '>':
         i = '>>'
         x += i
     elif i == '=':

         print(i)
         i = 'Equivalent'
         x += i

     else:
         x += i

print(fnc(x, True))

我要输入input =(a,b)而不是Equivalent function来呼叫(a=b)。我怎样才能做到这一点? 我尝试控制=的上一个和下一个元素,并在输入(a = b)时将其添加到Equivalent函数中,输出应为(a|~b)&(b|~a),但它不起作用。

i= sympy.Equivalent(symbols((inr[inr.index(i) - 1])), symbols(inr[inr.index(i) + 1]))

1 个答案:

答案 0 :(得分:0)

因此,根据您的示例I / O:

IN :因此,当您输入(a = b)时,输出应为? – user5173426

OUT :应该是(a |〜b)&(b |〜a)– Yacine Benatia

我对您现有的代码进行了一些更改:

from sympy import symbols, Equivalent
from sympy.logic.boolalg import to_cnf as fnc, Implies, to_cnf
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z


def eq(a, b):
    return fnc(Equivalent(a, b), True)

inr = str(input('Enter formula: '))
x = ' '
# get the part of the formula/string before =
y = inr.rsplit('=', 1)[0]
# get the part of the formula/string after =
z = inr.rsplit('=', 1)[1]
for i in inr:
     if i == '>':
         i = '>>'
         x += i
     elif i == '=':
         i = 'Equivalent'
         x = i
     elif i != ')':
         x += y
         x += ','
         x +=  z
     # print(x)

print(fnc(x, True))

输出:

OUT