I'm using a random number generater to select a question from a list at random, if the question has already been answered it should skip and reroll until it gets a number that hasn't been given yet.
It works until the options become too limited. It'll roll ~4 times. If it still doesn't have a number that hasn't been given before, it'll give an "index out of range" error.
Sample:
from random import randint
counter = int(0) # Max value, count the amount of questions in the list
done = [] # Already been rolled, ignore these values
list = open('questions.txt').readlines()
for l in list:
counter +=1
try:
# While there are less values in <done> than <counter>, roll and add to list
while len(done) < counter:
question = randint(1,counter)
while question in done:
print('Skipped [%i]' % question) # Check if ignored
question = randint(1,counter) # Reroll
else:
# Add to list so it knows the question has already been asked
done.append(question) # Add to list with given values
else:
print('Finished!\n')
except Exception as e:
print(e) # Show error if any
I have no clue what i've done wrong, please help.
Thanks :)
Aucun commentaire:
Enregistrer un commentaire