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

Skip enumeration and go to the next in 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 个帐户。我 运行 遇到的问题是当我 运行 检查我的一个帐户是否已经投票给 post.

的代码部分时

假设帐户 5 已经投票给 post:

当前代码完全停止了枚举,因此帐户 6 永远没有机会 运行 通过检查。

如果我将 return False 更改为 continue,账户 5 会尝试投票,因为它已经投票,异常也会完全停止脚本。

我已经尝试了breakpass等。我现在在想我可能遇到了缩进问题。当然,这很容易修复,我只是遗漏了一些东西。

您可能正在寻找继续而不是中断。 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

我假设 account[k] 只会在这里给出一个名字?