Reputation: 55
I am currently trying to get a list of questions and answers (QandA) and randomise the questions on every iteration. I know I can use choice() but I have only been taught random.randint() in terms of randomising. Can someone please help me on this? At this current state, a question and answer is asked but is not randomised. Tuples and other more advanced methods cannot be used because in my course I can only use what I am taught.
I have tried using random.randint(0, len(QandA) - 1) thinking this would randomise the list but it does not achieve the desired result.
import random
print("Welcome to Te Reo Maori Quiz!!!")
print("\nAnswer the questions with single Maori words")
QandA = ['A challenge laid down in chant and dance:','haka',
'What is "Sun" in Maori:', 'ra',
'Something you eat to fill your belly:', 'kai',
'Type in the Maori word for "cave":', 'ana',
'Traditional Maori food cooked in an earth oven:','hangi',
'Ma is white, whero is red, kakariki is green, pango is black. What else is black?:', 'mangu',
'its getting ... in here, so take off all your clothes:', 'wera',
'What does Kia ora mean?:', 'hello',
'What does ka pai mean?:', 'good',
'What does kei te peha koe mean?:', 'how are you',
'What is the Maori phrase for "what is your name?:', 'ko wai to ingoa',
'What does hikoi mean?:', 'walk',
'What is a waiata:', 'song',
'What is the the Maori word for stomach?:', 'puku',
'What does mahi mean?', 'work',
'What is the maori word for wait?:', 'taihoa',
'if something was nui, then it would be what?:', 'big',
'What does Haere mai mean? (hint: it starts with "w"):', 'welcome',
'What does nau mai mean?:', 'welcome',
'What does tangi mean?:', 'funeral',
]
points = 0
current = 0
quiz = 0
while(quiz < 5):
current = random.randint(0, len(QandA) - 1)
question = input("Q" + str(quiz + 1) + ". " + QandA[current])
newquestion = question.upper()
if question == QandA[current + 1]:
points = points + 10
current = current + 2
print("\nCorrect Answer!")
elif question == newquestion:
print("\nInvalid! Please enter only lowercase characters!")
current = current + 2
else:
print("\nIncorrect answer. The correct answer is:", QandA[current+1])
points = points - 10
current = current + 2
quiz = quiz + 1
if points < 0:
points = 0
print("End of Quiz.")
print("Your score: %", points, sep = "")
the expected result is to have the list randomise at every iteration and have the correct answer to that randomised question.
Upvotes: 2
Views: 102
Reputation: 10779
Here is a solution requiring only random.randint()
.
A random item from the list is popped out and appended to a new list. In this way, you will not have duplicates!
Mind that the original list gets deleted in the process but if you need it in the future, you can always make a copy of it.
import random
x = ['A','B','C','D','E','F']
rnd_x = []
while len(x):
try:
rnd_x.append(x.pop(random.randint(0,len(x))))
except IndexError:
pass
print(rnd_x)
#['F', 'B', 'C', 'D', 'A', 'E']
Upvotes: 0
Reputation: 341
I don't see a problem using random.randint(0, len(QandA) - 1)
to obtain a random index. The problem that you are having comes from your data sctructure. Having questions and answers as different elements will make it more difficult to guarantee that your index is a question.
Maybe you can change your array to array of tuples:
QandA = [('A challenge laid down in chant and dance:','haka'),
('What is "Sun" in Maori:', 'ra'),
('Something you eat to fill your belly:', 'kai'),
('Type in the Maori word for "cave":', 'ana'),
('Traditional Maori food cooked in an earth oven:','hangi'),
('Ma is white, whero is red, kakariki is green, pango is black. What else is black?:', 'mangu'),
('its getting ... in here, so take off all your clothes:', 'wera'),
('What does Kia ora mean?:', 'hello'),
('What does ka pai mean?:', 'good'),
('What does kei te peha koe mean?:', 'how are you'),
('What is the Maori phrase for "what is your name?:', 'ko wai to ingoa'),
('What does hikoi mean?:', 'walk'),
('What is a waiata:', 'song'),
('What is the the Maori word for stomach?:', 'puku'),
('What does mahi mean?', 'work)',
('What is the maori word for wait?:', 'taihoa'),
('if something was nui, then it would be what?:', 'big'),
('What does Haere mai mean? (hint: it starts with "w"):', 'welcome'),
('What does nau mai mean?:', 'welcome'),
('What does tangi mean?:', 'funeral'),
]
now you can do:
current = random.randint(0, len(QandA) - 1)
and then access it as:
q_a = QandA[current]
question = q_a[0]
answer = q_a[1]
Edit: if you can't use tuples the other option is to use 2 separate lists and have the Q&A with the same index.
current = random.randint(0, len(questions) - 1)
question = questions[current]
answer = answers[current]
Upvotes: 1