【发布时间】:2016-04-30 18:13:26
【问题描述】:
我正在尝试创建一个读取状态、转换和字符串的有限状态机。我正在尝试在没有对象的情况下创建它。一切正常,直到我的 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...
我的不同文本文件包含:
- states.txt
State2,1,0 状态3,0,1 状态4,1,0
- transitions.txt
状态1,0,状态2 状态2,1,状态3 状态3,0,状态4
- strings2.txt
10100101 1001 10010
【问题讨论】:
-
len(Strings)将返回一个整数 - 字符串的长度。你的意思是for i in Strings吗? -
for I in len (Strings):-->for I in range(len (Strings)):