0

I'm trying to loop through a list of values I got from database

I tried using a while loop to go through it while less than a number of times given by the user.

def winner(x):
    winner = random.choice(x)
    return winner_list(x, winner)

winner_lists = []
def winner_list(y, x):
    if x not in winner_lists:
        winner_lists.append(x)
    else:
        winner(y)

i = 0
competitors = User.query.all() #gotten from database
main_competitors = []
for competitor in competitors:
    competitor_raffles = competitor.raffles.split(',')
    if uuid in competitor_raffles:
        main_competitors.append(competitor.uuid.strip(' '))
while (i < form.number.data) and (main_competitors != []):
    winner(main_competitors)
    i+=1

I expect to see randomly chosen names from the list competitors

5
  • 1
    your function winner_list doesn't return anything. Commented Jun 6, 2019 at 1:52
  • I honestly understand what you mean because I just reviewed the code now, but I really do not know how to apply a fix because it is meant to stop iterating once 'i' is equal to the user input Commented Jun 6, 2019 at 2:08
  • why do you think you need recursion for this? You have a known maximum number of function calls. just pop the winner from the competitors list and forget recursion here. Commented Jun 6, 2019 at 2:09
  • K-K -- i never > 0 in your code. The recursion fails before i+=1. Commented Jun 6, 2019 at 2:10
  • I understand you and I tweaked it. I didn't need the functions after all. I worked with an if statement inside the while block. Thank you very much Commented Jun 6, 2019 at 2:18

2 Answers 2

1

It looks like the problem is in the while loop. When winner_lists has all of the main_competitors in it, then the functions winner and winner_lists keep calling each other since it is impossible to choose a new winner.

Maybe you forgot to remove a competitor from main_competitors each time winner is run.

Sign up to request clarification or add additional context in comments.

Comments

0

winner_list doesn't return a value to the caller. There's a bit to unpack here including the fact that you're using variable names that shadow your function names, among other things I'd change, but ultimately this is not a problem that requires recursion.

Why not? You know how many times to maximally choose a winner (from form.number.data), and you have a list of competitors of certain length.

This example chooses only unique winners. If a competitor can "win" more than once, then just remove the last line.

results = []
how_many_winners = form.number_data
contestants = main_competitors[:]
while main_competitors and len(results) < how_many_winners:
    this_winner = random.choice(contestants)
    results.append(this_winner)
    contestants.remove(this_winner)   # Remove this line if a contestant can 'win' more than once

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.