这是我的琐事游戏的代码,它应该提出5个问题,然后有4个多项选择答案,其中一个是正确的。用户应该猜测,然后显示最后的正确数量。我的问题是从10个问题列表中随机收集5个问题,然后从10个答案列表中收集相应的答案。我被指派在整个过程中使用一个问题类和实例,但我不相信我这样做是完全正确的。任何建议和例子都表示赞赏!
import random
class Question:
def __init__(self, question, answer1, answer2, answer3, answer4):
self.question = question
self.answer1 = answer1
self.answer2 = answer2
self.answer3 = answer3
self.answer4 = answer4
#defining the instances from Question
def setTriviaQuestion(self):
# randomly select 5 questions from the list
# display the 5 questions
quest_list = ['What is the only manmade object observable from the moon?','On which island was Napolean exiled following his defeat at Waterloo?', 'What is the capital of Australia?','Who was the "Mad Monk" of modern history?','What is the largest fish in the ocean?', 'Which artist painted a mustache and a goatee on the Mona Lisa?','In what country would one find 8 of the world\'s 10 largest mountains?', 'Which common word changes in pronounciation when the first letter is capitalized?','Which is the only vowel on the standard keyboard that is not on the top line of letters?', 'What is the most popular drink in the world that does not contain alcohol?']
selected_quests = []
for quest_list in random.sample(quest_list, 5):
selected_quests.append(quest_list)
print (selected_quests)
def setAnswers(self):
# select the answer from the list based off of the which questions were chosen
# answer order corresponds to the questions
# connect randomly chosen questions to their answer -- how?
ans_list = ['The Great Wall of China','St. Helena','Canberra','Rasputin','A Whale Shark','Marcel Duchamp','Nepal', 'Polish','A','Coffee']
answer1 = ans_list[q] # q is q from above
print (answer1)
# the next 3 answers are chosen randomly from ans_list
answer2 = ans_list[random.randint(1,10)]
print (answer2)
answer3 = ans_list[random.randint(1,10)]
print (answer3)
answer4 = ans_list[random.randint(1,10)]
print (answer4)
# also setAnswer1 from above
correctAnswer = random.randstr(asked_quest)
# judge whether the user input is correct or not depending on whether it is the same as setCorrectAnswer
userAnswer = raw_input(print(q))
if userAns.lower == correctAnswer.lower:
def setNumCorrect():
return True
else:
def setNumCorrect():
return False
# if user input is correct, add to the amount of correct guesses overall
numCorrect += 1
def getAnswer1():
return answer1
def getAnswer2():
return answer2
def getAnswer3():
return answer3
def getAnswer4():
return answer4
def start(self):
print("Welcome to Trivia!")
self.setTriviaQuestion()
self.setAnswer1(A)
self.setAnswer2(B)
self.setAnswer3(C)
self.setAnswer4(D)
print (self.getAnswer1())
print (self.getAnswer2())
print (self.getAnswer3())
print (self.getAnswer4())
self.setCorrectAnswer()
print(Question.question)
print(Question.answer1)
print(Question.answer2)
print(Question.answer3)
print(Question.answer4)
print(Question.numCorrect)