Python 试图实现DFA,但代码正在生成错误

Python 试图实现DFA,但代码正在生成错误,python,dfa,Python,Dfa,代码:- #Shortest DFA implementation in Python S,D,F=input() s=1 for c in S:s=D[s,c] print(["Not a chance!","Accepted!"][F&s>0]) --------------------------------------------------------------------------- ValueError

代码:-

#Shortest DFA implementation in Python
S,D,F=input()
s=1
for c in S:s=D[s,c]
print(["Not a chance!","Accepted!"][F&s>0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-506f09a31940> in <module>()
      1 #Shortest DFA implementation in Python
----> 2 S,D,F=input()
      3 s=1
      4 for c in S:s=D[s,c]
      5 print(["Not a chance!","Accepted!"][F&s>0])

ValueError: too many values to unpack (expected 3)
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-7e5418d2d6fc> in <module>()
      4 for c in S:
      5  t,s=s,0
----> 6  for a,b in D[c]:s|=t/a%2*b
      7 print(["Not a chance!","Accepted!"][F&s>0])

TypeError: unsupported operand type(s) for |=: 'int' and 'float'
输入:- 输入是字符串S、delta函数D和最终状态掩码F的三元组。I以2的幂对每个状态进行编号,因此F只是每个接受状态的OR。D是来自(状态,输入字符)->状态的映射

输入示例(接受以b结尾的所有字符串):

输出:-

#Shortest DFA implementation in Python
S,D,F=input()
s=1
for c in S:s=D[s,c]
print(["Not a chance!","Accepted!"][F&s>0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-506f09a31940> in <module>()
      1 #Shortest DFA implementation in Python
----> 2 S,D,F=input()
      3 s=1
      4 for c in S:s=D[s,c]
      5 print(["Not a chance!","Accepted!"][F&s>0])

ValueError: too many values to unpack (expected 3)
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-7e5418d2d6fc> in <module>()
      4 for c in S:
      5  t,s=s,0
----> 6  for a,b in D[c]:s|=t/a%2*b
      7 print(["Not a chance!","Accepted!"][F&s>0])

TypeError: unsupported operand type(s) for |=: 'int' and 'float'
输入:- 我们像以前一样,将状态数为2的幂。D是从输入字符到由该字符标记的转换列表的映射

输入示例(接受以b结尾的所有字符串):

获得的输出为:-

#Shortest DFA implementation in Python
S,D,F=input()
s=1
for c in S:s=D[s,c]
print(["Not a chance!","Accepted!"][F&s>0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-506f09a31940> in <module>()
      1 #Shortest DFA implementation in Python
----> 2 S,D,F=input()
      3 s=1
      4 for c in S:s=D[s,c]
      5 print(["Not a chance!","Accepted!"][F&s>0])

ValueError: too many values to unpack (expected 3)
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-7e5418d2d6fc> in <module>()
      4 for c in S:
      5  t,s=s,0
----> 6  for a,b in D[c]:s|=t/a%2*b
      7 print(["Not a chance!","Accepted!"][F&s>0])

TypeError: unsupported operand type(s) for |=: 'int' and 'float'
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)],2
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
4对于S中的c:
5t,s=s,0
---->对于a,b在D[c]:s |=t/a%2*b
7打印([“不可能!”,“接受!”][F&s>0])
TypeError:|=:“int”和“float”的操作数类型不受支持

请告诉我上述代码中所需的必要但最小的修改,以使其成功执行并产生所需的输出

此代码在Python 2中有效,而在Python 3中无效。我怀疑当您使用Python3时,代码实际上是为Python2设计的。在Python2中,它不仅从输入读取字符串,还执行一条语句,这意味着它将获取字符串并将其转换为Python语法。在Python3中,返回输入内容的字符串。不再像在Python 2中看到的那样执行
eval
。要模拟Python 2中的内容,需要在
input
上运行
eval

In [1]: S,D,F = eval(input())
'abab',{(1,'a'):1,(1,'b'):2,(2,'a'):1,(2,'b'):2},2

In [2]: S
Out[2]: 'abab'

In [3]: D
Out[3]: {(1, 'a'): 1, (1, 'b'): 2, (2, 'a'): 1, (2, 'b'): 2}

In [4]: F
Out[4]: 2
编辑 对于正在运行的新代码,罪魁祸首是循环中的除法操作:
t/a
。默认情况下,它返回一个浮点数,而在Python 2中,如果
t
a
都是整数,它将返回一个整数。在Python3环境中运行Python2代码时必须小心。只需将
t/a
包装在
int
调用中:
int(t/a)

我最后要提醒您的是,在Python3环境中运行Python2代码时要格外小心。如果可能的话,在设计的环境中运行代码。如果不是,这个有用的Python 2到Python 3的习惯用法比较指南应该会有所帮助:。特别是:

  • 输入
  • 。。。最后是除法运算符:

#最短NFA实现S,D,F=eval(input())S=1表示S中的c:t,S=S,0表示D中的a,b[c]:S |=t/a%2*b print([“不可能!”,“接受!”][F&S>0])@AkashDas我运行了这段代码,并获得了
接受作为输出。我对DFA程序进行了eval()修改,它运行良好。我所评论的代码是用于实现NFA的。我已相应地修改了问题。我正在使用Python3。我也提到了获得的错误。如果你有什么想法,请你看一下,告诉我好吗。谢谢<默认情况下,code>t/a
返回一个浮点数,而在Python 2中,如果
t
a
都是整数,它将返回一个整数。在Python3环境中运行Python2代码时必须小心。只需将
t/a
包装在
int
调用中:
int(t/a)
。我将编辑我的帖子。非常感谢你的帮助。