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

时间:2017-06-19 16:00:51

标签: python loops enumerate continue

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))

您好, 我正在使用voting bot进行类似Reddit的使用。该机器人控制6个帐户。我遇到的问题是当我运行代码段来检查我的一个帐户是否已经投票支持该帖子。

假设帐户5已经投票支持该帖子:

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

如果我将return False更改为continue,则帐户5会尝试投票,并且因为它已经投票,该例外也会完全停止该脚本。

我已经尝试了breakpass等等。我现在想我可能会有缩进问题。当然这是一个简单的解决方案,我只是遗漏了一些东西。

1 个答案:

答案 0 :(得分:0)

你可能正在寻找继续而不是休息。 https://docs.python.org/3/reference/simple_stmts.html#continue 由于继续只是从内在突破,你可能能够重新构造内部作为发电机。

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

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