跳过枚举并转到下一个:在Python中

2024-06-28 11:42:18 发布

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

def worker(worker_comment):
    time.sleep(vote_delay)
    try:
        for (k, v) in enumerate(account):
            worker_steem = Steem(keys=posting_key)
            upvote_comment = worker_steem.get_content(worker_comment.identifier)

            # Checking if Post is flagged for Plagarism & Spam
            names = ['blacklist', 'fubar-bdhr']
            if ('-' in str(upvote_comment['author_reputation'])):
                print("@%s %s ====> Upvote Skipped - Author rep too low")
                return False
            for avote in upvote_comment['active_votes']:
                if (avote['voter'] in names and avote['percent'] < 0):
                    print("@%s %s ====> Upvote Skipped - Flagged by blacklist or Fubar-bdhr")
                    return False

            # Checking if there is markings from Cheetah or Steemcleaners:
            names = ['cheetah', 'steemcleaners']
            for avote in upvote_comment['active_votes']:
                if (avote['voter'] in names):
                    print("@%s %s ====> Post Marked by SteemCleaners or Cheetah")
                    return False

            # Checking if the voting power is over 60%:
            if (mainaccount['voting_power'] < 60):
                print("Not Enough Voting Power")
                return False

            # Checking if we already voted for this post:
            names = account[k]
            for avote in upvote_comment['active_votes']:
                if (avote['voter'] in names):
                    print("@%s %s ====> Post Upvoted already by", account[k])
                    return False

            # UPVOTING THE POST
            upvote_comment.vote(100, voter=account[k])

            # Upvote has now been done and it will now print a message to your screen:
            print(upvote_comment, " ====> UPVOTED by", account[k])
            print("Voting Power Left:", mainaccount['voting_power'])
            upvote_history.append(upvote_comment.identifier)
    except:
        print("ERROR - Upvoting failed for", account[k])
        print("We have probably already upvoted this post before the author edited it.")
        print(str(e))

你好, 我正在开发一个类似Reddit的voting bot。这个机器人控制6个帐户。我遇到的问题是,当我运行一段代码来检查我的一个帐户是否已经投票支持这个职位时。你知道吗

假设账户5已经投票支持该职位:

当前代码完全停止了枚举,因此Account6永远没有机会运行检查。你知道吗

如果我将return False更改为continue,帐户5将尝试投票,因为它已经投票了,所以异常也会完全停止脚本。你知道吗

我试过breakpass,等等。我现在想我可能有缩进问题。当然,这是一个简单的解决办法,我只是错过了一些东西。你知道吗


Tags: infalseforbyreturnifnamescomment
1条回答
网友
1楼 · 发布于 2024-06-28 11:42:18

你可能在寻找继续而不是休息。 https://docs.python.org/3/reference/simple_stmts.html#continue 由于continue只会从内部for中断,因此可以将内部for重新构造为生成器。你知道吗

 if account[k] in [avote['voter'] for avote in upvote_comment['active_votes']]:
    print("@%s %s ====> Post Upvoted already by",  account[k])
    continue

我假设帐户[k]在这里只给出一个名字?你知道吗

相关问题 更多 >