所以我试图用python编写测验。应该有15个问题,随机得到5个(无重复),并一一询问用户。我将为每个问题预先定义4个选择答案。
我的方法是从存储在列表中的15个随机数中提取5个,并将其放入新列表中。如何检查random_questions [0]-[4]的每个索引的值是否等于任何索引问题[14],以获取随机问题的哪个索引与原始15个问题的哪个索引相同,因此我可以提示程序打印出相同的答案列表索引。因此问题[0]的问题将得到答案[0]的答案,但用户将获得random_question [0],该值将与问题[0]相同,因为它是随机选择的。也许我的方法是错误的,所以任何指导都将有所帮助。谢谢
print("***WELCOME TO THE RANDOM QUIZ***")
import random
questions = ['What is the capital city of Ireland?', 'What is the capital city of France', 'What is the capital city of Germany', 'What is the capital city of Romania', 'What is the capital city of Russia', 'What is the capital city of Belgium', 'What is the capital city of Switzerland','What is the capital city of Egypt','What is the capital city of Japan','What is the capital city of China','What is the capital city of Taiwan','What is the capital city of Brazil', 'What is the capital city of Argentina','What is the capital city of Bolivia','What is the capital city of Panama',]
num_of_rand_q = 5
#right_q_count = 0
list_of_rand_q = random.sample(questions, num_of_rand_q)
first_rand_q = list_of_rand_q[0]
second_rand_q = list_of_rand_q[1]
third_rand_q = list_of_rand_q[2]
fourth_rand_q = list_of_rand_q[3]
fifth_rand_q = list_of_rand_q[4]
答案 0 :(得分:0)
数据结构的选择可能更好。无需将问题和答案存储在两个单独的列表中,而是在选择了五个随机问题之后,不得不弄乱一些索引,只需将对 (question, answer)
存储在列表中,然后选择五个这样的< em> pairs 。您还可以使用字典:
data = {
"What planet is the source of lots of jokes?": "Uranus",
# question: answer pairs
}
# choose random questions:
q = random.sample(data, 5)
for question in q:
user_answer = ...
if user_answer == data[q]:
print("Yeah!")
答案 1 :(得分:0)
使用questions.index(first_rand_q)
获取该问题相对于您已定义的问题组的索引。假设您的问题列表直接映射到您的答案列表,则可以执行answers[the_index_you_just_found]
。
示例:
print("***WELCOME TO THE RANDOM QUIZ***")
import random
questions = ['What is the capital city of Ireland?', 'What is the capital city of France', 'What is the capital city of Germany', 'What is the capital city of Romania', 'What is the capital city of Russia', 'What is the capital city of Belgium', 'What is the capital city of Switzerland','What is the capital city of Egypt','What is the capital city of Japan','What is the capital city of China','What is the capital city of Taiwan','What is the capital city of Brazil', 'What is the capital city of Argentina','What is the capital city of Bolivia','What is the capital city of Panama',]
answers = ['']*15 #add answers to your questions here in the same order as the questions
num_of_rand_q = 5
list_of_rand_q = random.sample(questions, num_of_rand_q)
list_of_rand_a = [] #create a list of answers to the 5 random questions
first_rand_q = list_of_rand_q[0]
second_rand_q = list_of_rand_q[1]
third_rand_q = list_of_rand_q[2]
fourth_rand_q = list_of_rand_q[3]
fifth_rand_q = list_of_rand_q[4]
for question in list_of_rand_q:
q_index = questions.index(question) #get index of question
list_of_rand_a.append(answers[q_index]) #index of question in original question list should match index in answers list so we use it to get our desired answer
print(list_of_rand_a)
答案 2 :(得分:0)
存储您的答案,进行适当的检查以避免索引超出范围的异常,然后尝试
print("***WELCOME TO THE RANDOM QUIZ***")
import random
questions = ['What is the capital city of Ireland?', 'What is the capital city of France', 'What is the capital city of Germany', 'What is the capital city of Romania', 'What is the capital city of Russia', 'What is the capital city of Belgium', 'What is the capital city of Switzerland','What is the capital city of Egypt','What is the capital city of Japan','What is the capital city of China','What is the capital city of Taiwan','What is the capital city of Brazil', 'What is the capital city of Argentina','What is the capital city of Bolivia','What is the capital city of Panama',]
answers=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O']
num_of_rand_q = 5
#right_q_count = 0
list_of_rand_q = random.sample(questions, num_of_rand_q)
first_rand_q = list_of_rand_q[0]
second_rand_q = list_of_rand_q[1]
third_rand_q = list_of_rand_q[2]
fourth_rand_q = list_of_rand_q[3]
fifth_rand_q = list_of_rand_q[4]
for i in list_of_rand_q:
print(i,'Answer :',answers[questions.index(i)])