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

Hi, I am working on a voting bot for a Reddit-like use. This bot controls 6 accounts. The problem I'm running into is when I run the section of code that checks if one of my accounts have already voted for the post.

Let's say account 5 has already voted for the post:

The current code stops the enumeration altogether, so account 6 never gets a chance to run through the checks.

If I change the return False to continue, account 5 attempts to vote and because it has already voted, the exception also stops the script altogether.

I have tried break, pass, etc. I'm thinking now that I may have an indentation problem. Surely it is an easy fix and I'm just missing something.

Raj Subit
  • 1,487
  • 2
  • 12
  • 23

1 Answers1

0

You might be looking for continue rather than break. https://docs.python.org/3/reference/simple_stmts.html#continue As continue only breaks out of the inner for, you might be able to re-structure the inner for as a generator.

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

I'm assuming that account[k] will only give one name here?

Mic
  • 331
  • 1
  • 4
  • '# 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 So in the instance above, I changed return False to continue, but continue just puts it into the for loop above the if loop, which leads it to the 'upvote_comment.vote(100, voter=account[k])' – Jared Willis Jun 19 '17 at 16:57
  • You're right, continue only continues with the innermost loop. I've added a differrent method of checking the voter account which might be what you're looking for. – Mic Jun 19 '17 at 17:21
  • The problem I have is that if I 'return False', the enumeration stops and any iterations that have not run, won't run. I don't have a problem checking the voter account, it's just that I can't figure out a way to skip to the next enumeration when it comes back already voted. – Jared Willis Jun 19 '17 at 17:31
  • My bad, I meant to say continue. I've corrected the code above. – Mic Jun 19 '17 at 17:37
  • By the way, this looks like it will vote with all (six) of the accounts (except any that have already voted). Is that what you wanted? (Just curious). – Mic Jun 19 '17 at 18:04
  • I should point out there are other ways you can avoid the nested for. I believe the standard recommendation is to refactor the inner for into a separate function. That way the return doesn't exit the main function, and you can test the result of calling this function in the main loop body. See https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python – Mic Jun 19 '17 at 18:25