這是一個基於文本的測驗示例,其中程序應給出5個問題供用戶以隨機順序回答。問題在於它完美地工作,但只給出3個隨機問題,然後停止。Python基於文本的隨機問題測驗
import random
question1=["The answer is A","A","a"]
question2=["The answer is B","B","b"]
question3=["The answer is A","A","a"]
question4=["The answer is F","F","f"]
question5=["The answer is A","A","a"]
questions=[question1,question2,question3,question4,question5]
used_questions=[]
while len(used_questions)!=len(questions):
random_question=random.choice(questions)
while random_question in used_questions:
random_question=random.choice(questions)
used_questions.append(random_question)
print([random_question[0]])
players_answer=input("")
if players_answer in random_question:
print("\nCorrect!")
else:
print("\nWrong!")
你的方法比較複雜,只要做'random.shuffle(問題)'並遍歷問題結果列表(現在以隨機順序) –
因爲你的一些問題是**相同**,所以在三個問題,還沒有問題還沒有被問到。所以你的while while random_questions在used_questions循環中變成了一個無限循環。 – khelwood