我试图创建一个程序,从txt文件中生成带有用户名的文本,但我一直得到TypeError: 'int' object is not iterable
。我知道这意味着什么,但我不知道如何解决我的问题。我试着只做y = 12 / 2
,当我通过循环y
时出现了同样的错误,我真的很困惑,所以如果有人能帮助我,那将是很棒的
这是我的代码
def generateNum():
#imports random
from random import randint
for _ in range(10):
value = randint(0, 900000)
return(str(value))
def getNumOfLines( file):
#opens txt file
with open(file) as f:
Lines = f.readlines()
count = 0
# Strips the newline character
for line in Lines:
count += 1
return(count)
class debug:
def __init__(self, credsTxt, tagsTxt):
self.credsTxt = credsTxt
self.tagsTxt = tagsTxt
self.numOfCreds = getNumOfLines(credsTxt)
self.numOfTags = getNumOfLines(tagsTxt)
self.ammountPerAccount = round(self.numOfTags / self.numOfCreds)
def getComments(self):
#initializes comment
comment = ""
#opens txt file
file1 = open(self.tagsTxt, 'r')
count = 0
while True:
count += 1
# Get next line from file
line = file1.readline()
for i in self.ammountPerAccount:
# if line is empty
# end of file is reached
if not line:
break
comment += ' ' + line.strip() + ' ' + generateNum() + '.'
return(comment)
print(debug('D:/FiverrWork/user/instagram-bot/textGen/assets/login_Info.txt', 'D:/FiverrWork/user/instagram-bot/textGen/assets/tags.txt').getComments())
这是我的堆栈跟踪错误
Traceback (most recent call last):
File "d:FiverrWorkusertextgeneratortextgeneratortxt.py", line 57, in <module>
print(debug('D:/FiverrWork/user/textgenerator/textgenerator/assets/login_Info.txt', 'D:/FiverrWork/user/textgenerator/textgenerator/assets/tags.txt').getComments())
File "d:FiverrWorkusertextgeneratortextgeneratortxt.py", line 47, in getComments
for i in self.ammountPerAccount():
TypeError: 'int' object is not callable
发布的for
循环无法在int
上迭代。您打算在range()
:上进行迭代
for _ in range(self.ammountPerAccount):
# if line is empty
# end of file is reached
if not line:
break
comment += ' ' + line.strip() + ' ' + generateNum() + '.'
我使用_
作为占位符变量,因为每次都没有使用i
的实际值。