我想使用 random.randint() 使用列表进行随机测验。截至目前,我的代码运行良好,但有时它会向用户询问两次问题,而不是随机分配所有问题。这只是有时发生。如何仅使用 random.randint 来避免这种情况?我只能使用 this 和 .del(),因为这是我在类(class)中教授的内容。
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 = "")
我希望我的代码在每次迭代时产生 5 个随机问题。
请您参考如下方法:
这是一个在 while 循环中避免重复的例子。
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]]
只有 random.randint() 允许的解决方案。
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)
然而这个解决方案包含以下知识:
- 列表的副本
CQ = Q[:] - 在列表中找到一个项目的索引
Q.index(CQ[x])
