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

Change position of input to call a function in python

这是我的代码:

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))

我想输入 (a=b) 而不是 input =(a,b) 来调用 Equivalent function。我怎样才能做到这一点? 我试图控制 = 之前和之后的元素,并在输入 (a=b) 时将其添加到 Equivalent 函数,输出应该是 (a|~b)&(b|~a) 但它不起作用。

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

因此,根据您的示例 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))

输出: