Reputation: 55
I am wanting to use random.randint() to make a random quiz using list. As of now my code works well but there are times where it will ask the user a question twice instead of randomising all questions. This happens only sometimes. How do I avoid this by only using random.randint? I can only use this and .del() as this is what I am taught in my course.
import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
Q = ['A challenge laid down in chant and dance:',
'What is "Sun" in Maori:',
'Something you eat to fill your belly:',
'Type in the Maori word for "cave":',
'Traditional Maori food cooked in an earth oven:',
'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:',
'its getting ... in here, so take off all your clothes:',
'What does Kia ora mean?:',
'What does ka pai mean?:',
'What does kei te peha koe mean?:',
'What is the Maori phrase for "what is your name?:',
'What does hikoi mean?:',
'What is a waiata:',
'What is the the Maori word for stomach?:',
'What does mahi mean?',
'What is the maori word for wait?:',
'if something was nui, then it would be what?:',
'What does Haere mai mean? (hint: it starts with "w"):',
'What does nau mai mean?:',
'What does tangi mean?:',
]
A = ['haka', 'ra', 'kai', 'ana', 'hangi', 'mangu', 'wera', 'hello', 'good', 'how are you', 'ko wai to ingoa', 'walk',
'song', 'puku', 'work', 'taihoa', 'big', 'welcome', 'welcome', 'funeral'
]
points = 0
current = 0
quiz = 0
while (quiz < 5):
question = Q[current]
answer = A[current]
question = input("\nQ" + str(quiz + 1) + ". " + Q[current])
if question.lower() == answer.lower():
points = points + 20 #adds points if the answer is correct
current = current + 2
print("Correct Answer!")
else:
print("Incorrect answer. The correct answer is:", A[current])
###points = points - 10
###current = current + 2
#quiz = quiz + 1
###if points < 0:
###points = 0
print("\nEnd of Quiz.")
print("Your score: %", points, sep = "")
I expect my code to produce 5 random questions at every iteration.
Upvotes: 2
Views: 392
Reputation: 4189
Here is an example to avoid duplication in while loop.
Q = ['a', 'b', 'c', 'd', 'e']
while len(Q)!= 0:
x = random.randint(0,len(Q)-1)
print(Q[x])
Q = [v for v in Q if v != Q[x]]
A only random.randint()
allowed solution.
import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
Q = ['A challenge laid down in chant and dance:',
'What is "Sun" in Maori:',
'Something you eat to fill your belly:',
'Type in the Maori word for "cave":',
'Traditional Maori food cooked in an earth oven:',
'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:',
'its getting ... in here, so take off all your clothes:',
'What does Kia ora mean?:',
'What does ka pai mean?:',
'What does kei te peha koe mean?:',
'What is the Maori phrase for "what is your name?:',
'What does hikoi mean?:',
'What is a waiata:',
'What is the the Maori word for stomach?:',
'What does mahi mean?',
'What is the maori word for wait?:',
'if something was nui, then it would be what?:',
'What does Haere mai mean? (hint: it starts with "w"):',
'What does nau mai mean?:',
'What does tangi mean?:',
]
A = ['haka', 'ra', 'kai', 'ana', 'hangi', 'mangu', 'wera', 'hello', 'good', 'how are you', 'ko wai to ingoa', 'walk',
'song', 'puku', 'work', 'taihoa', 'big', 'welcome', 'welcome', 'funeral' ]
CQ = Q[:]
points = 0
quiz = 0
while (quiz < 5):
x = random.randint(0, len(CQ)-1)
print(str(quiz + 1) + ". " + CQ[x])
UA = input("\n " )
if A[Q.index(CQ[x])].lower() == UA.lower():
points = points + 20 #adds points if the answer is correct
print("Correct Answer!")
else:
print("Incorrect answer. The correct answer is:", A[Q.index(CQ[x])])
quiz += 1
del CQ[x]
print("\nEnd of Quiz.")
print("Your score: ", points)
Yet this solution contains knowledge about:
CQ = Q[:]
Q.index(CQ[x])
Upvotes: 1
Reputation: 10957
You should reduce the pool by the element you drew on each iteration, otherwise your routine will decrease in efficiency.
Imagine drawing 99 elements from a list of 100 without repetition. If you draw from range(100)
every time and then check if you already drew this number before, it will take you more and more time to draw a new valid number.
Simply create a random sequence of indices:
import random
samples = range(len(Q))
random.shuffle(samples)
and then whenever you want a new random question, remove an element from the list of indices and get the corresponding question:
Q[samples.pop()]
You are sure to not draw any question twice and you do not alter the order of the list of questions. Problem solved.
Upvotes: 0
Reputation: 7510
Just shuffle your questions, and then iterate through them:
import random
Q = ['a', 'b', 'c', 'd', 'e']
random.shuffle(Q)
print(Q)
Upvotes: 0