Python“str”对象不是callab

2024-04-25 18:57:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码中有一个严重的错误,我自己无法解决。这是我的代码:

    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)

当我尝试运行此程序时,出现以下错误:

^{pr2}$

还有,是的,我确实试过查找问题,但没有找到任何可以帮助我解决我的特殊情况。提前谢谢,祝你今天愉快


Tags: key代码txtoutputmystatus错误code
2条回答

my_accounts是一个字符串:

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

但你想把它当作一个函数:

^{pr2}$

如果你也有一个使用相同名称的函数,你必须重命名其中一个,你不能对一个变量和一个函数使用相同的名称。在

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()的内容。在

相关问题 更多 >