2016-04-14 182 views
0

我的代码中有一个令人讨厌的错误,我无法自己弄清楚。这里是我的代码:Python'str'对象不可调用

accounts = open('usernames.txt').read().splitlines() 
    my_accounts = random.choice(accounts) 
    for x in my_accounts(starting_account, ending_account, 1): 
     payload = { 'user_key': get_user_key(), 
        'terms': 'true', 
        'action': 'edit', 
        'page': 'simple', 
        'flow': 'TestA', 
        'dob': '1987-22-01', 
        'gender': 'f', 
        'name': str(x), 
        'password': create_password()} 
     r = requests.post(CONST_URL + end_point, headers=headers, cookies=cookies, data=payload, allow_redirects=False, verify=False) 

     if r.status_code == 302: 
      accounts_output = 'accounts.txt' 
      f = open(accounts_output, 'w') 
      user_output = (str(r.status_code) + ' Account created succesfully: ' + str(x) + ' ' + create_password()) 
      f.write(user_output) 
      f.close() 
     else: 
      print(str(r.status_code) + ' Unable to connect to the server :/') 
      print(r.content) 

当我尝试运行此我得到以下错误:

Traceback (most recent call last): 
    File "C:/Users/Google Drive/testing/moreTesting.py",    line 66, in <module> 
ms = accountCreator().account_creator(0, 5) 
    File "C:/Users/Pieperloy/Google Drive/testing/moreTesting.py",  line 43, in account_creator 
    for x in my_accounts(starting_account, ending_account, 1): 
TypeError: 'str' object is not callable 

而且,是的,我曾尝试查找问题,但没有发现任何东西,可以帮助我与我的具体情况。在此先感谢,祝你有个美好的一天

+0

嗯......你确定'str'没有被定义为某个变量吗?否则,您的代码应该工作 – Zizouz212

+0

您的错误消息和这里发布的代码都参考不同的代码!你的错误信息包含'str(ending_account)'和'str(starting_account)',你在这里写的代码不会。请发布正确的代码片段。 –

+0

@soon:my_accounts呢? –

回答

1
accounts = open('usernames.txt').read().splitlines() 
my_accounts = random.choice(accounts) 
for x in my_accounts(starting_account, ending_account, 1): 

splitlines()返回字符串列表。因此,您的my_accounts变量将从您的accounts列表中随机选择一个字符串。

因此,当您在for循环中调用my_accounts()时,您会收到错误str objects are not callable

了解更多关于splitlines()

3

my_accounts是一个字符串:

accounts = open('usernames.txt').read().splitlines() 
my_accounts = random.choice(accounts) 

,但你要使用它作为一个功能:

my_accounts(str(starting_account), str(ending_account), 1) 

如果你也有使用完全相同的名称的功能,你就会有要重命名一个或另一个,不能为变量和函数使用相同的名称。

+0

哦。好的赶上! :D – Zizouz212

+0

嗯,这很有道理,那么我将如何去获取for循环中的my_account变量? 'usernames.txt'包含多行字符串我想从该文本文件中获取一定数量的行,然后提交。 starting_account&ending_account只是整数,所以我可以说'嘿,让我0到5个帐户',如果这是有道理的? – Naomi

+0

你不需要随机选择账户。您可以将for循环更改为'for account in [starting_account:ending_account - starting_account]:'。这被称为列表切片。看看它。 – ronakg