gpt4 book ai didi

python - 在一个简单的有限状态机中,错误 : 'int' object is not iteratable

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:20 26 4
gpt4 key购买 nike

我正在尝试创建一个读取状态、转换和字符串的有限状态机。我试图在没有对象的情况下创建它。一切正常,直到我的 for 循环。但是,一旦循环开始,我就会收到错误消息:

line 42, in <module>
for I in len (Strings):
TypeError: 'int' object is not iterable

为什么会这样?任何提示将不胜感激。

Sfile = open("states.txt","r")
States = []

ReadLine = Sfile.readline()
while ReadLine != "":
A, B, C = ReadLine.split(",")
States.append((A, bool(int(B)), bool(int(C))))
ReadLine = Sfile.readline()
print States, "\n"
Sfile.close()



Tfile = open("transistions.txt","r")
Transitions = []

ReadLine = Tfile.readline()
while ReadLine != "":
ReadLine = ReadLine.rstrip()
Tran4, Tran5, Tran6 = ReadLine.split(",")
Transitions.append((Tran4, Tran5, Tran6))
ReadLine = Tfile.readline()
print Transitions
Tfile.close()

Strfile = open("strings2.txt","r")
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
Readline = ReadLine.rstrip()
Strings.append(Readline)
ReadLine = Strfile.readline()

print Strings, '\n'
Strfile.close()


for I in len (Strings):
for C in Strings[I]:
Start = '0'
Current = Start
if C in Strings == '0':
Current = A
else:
Current = State

print Current...

我的不同文本文件包含:

  • 状态.txt

状态 2,1,0状态 3,0,1状态4,1,0

  • transitions.txt

状态1,0,状态2状态2,1,状态3状态3,0,状态4

  • strings2.txt

10100101100110010

最佳答案

您不能遍历整数。我认为您的意思是遍历 range 对象 range(len(Strings))。这将起作用,因为 range 对象是可迭代的,而 int 不是。

关于python - 在一个简单的有限状态机中,错误 : 'int' object is not iteratable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com