TypeError:“int”对象在python中是可订阅的
我正在写一个程序,看看一个单词中是否有三个连续的双字母,就像簿记一样 当我试图运行程序时,我得到一个错误,TypeError:“int”对象是可订阅的。有什么原因吗TypeError:“int”对象在python中是可订阅的,python,string,file,Python,String,File,我正在写一个程序,看看一个单词中是否有三个连续的双字母,就像簿记一样 当我试图运行程序时,我得到一个错误,TypeError:“int”对象是可订阅的。有什么原因吗 def find_word(string): count = 0 for eachLetter in range(len(string)): if eachLetter[count] == eachLetter[count + 1] and eachLetter[count+ 2] == eachL
def find_word(string):
count = 0
for eachLetter in range(len(string)):
if eachLetter[count] == eachLetter[count + 1] and eachLetter[count+ 2] == eachLetter[count + 3] and eachLetter[count+ 4] == eachLetter[count + 5]:
print string
else:
count = count + 1
def main():
try:
fin = open('words.txt') #open the file
except:
print("No file")
for eachLine in fin:
string = eachLine
find_word(string)
if __name__== '__main__':
main()
您的循环:
for eachLetter in range(len(string)):
将0到1之间小于字符串长度的数字分配给变量eachLetter;在这之后,每个人[计数]都毫无意义
你是说字符串[每一个]等吗
注意,您还将得到索引错误;例如,当你到达簿记的第8个字母时,没有字符8+5=13需要检查,你的程序就会崩溃
由于这似乎是家庭作业,我将把它作为一个练习留给您,让您找出如何更快地停止循环5个字符。以下是您的错误:
if eachLetter[count]
这里每个参数都是int,因为range返回int的列表。您应该发布完整的回溯。这次很明显,但请在以后的问题中包含确切的错误消息。注意,您可以像在fin中为每条线编写代码一样,用字符串为每条线编写代码。如果你也想要这封信的索引,试试索引吧,信在里面谢谢!对于那些可能正在阅读问题以获得答案的人,要修复错误,请执行以下操作:rangelenstring-1如果您解释了您提供的代码块的作用,您的答案会更好。
fin = open('words.txt')
string = fin.readline()
def find_word(string):
for string in fin:
count = 0
for count in range(len(string)-5):
if string[count] == string[count + 1] and string[count+ 2] == string[count + 3] and string[count+ 4] == string[count + 5]:
print(string)
def main(fin):
for string in fin:
return find_word(string)
main(fin)