我目前正在尝试获取问题和答案列表 (QandA),并在每次迭代时随机分配问题。我知道我可以使用 choice() 但我只在随机化方面被教导过 random.randint() 。有人可以帮我吗?在当前状态下,会提出一个问题和答案,但不是随机的。不能使用元组和其他更高级的方法,因为在我的类(class)中我只能使用我所教的内容。
我曾尝试使用 random.randint(0, len(QandA) - 1) 认为这会使列表随机化,但它没有达到预期的结果。
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 = "")
预期的结果是让列表在每次迭代时随机化,并对该随机化的问题给出正确答案。
请您参考如下方法:
我没有看到使用 random.randint(0, len(QandA) - 1) 获取随机索引的问题。您遇到的问题来自您的数据结构。将问题和答案作为不同的元素会更难保证您的索引是一个问题。
也许您可以将数组更改为元组数组:
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'),
]
现在你可以:
current = random.randint(0, len(QandA) - 1)
然后访问它:
q_a = QandA[current]
question = q_a[0]
answer = q_a[1]
编辑: 如果您不能使用元组,另一种选择是使用 2 个单独的列表并使用相同的索引进行问答。
current = random.randint(0, len(questions) - 1)
question = questions[current]
answer = answers[current]
