2

Here is my code for a trivia game that is supposed to present 5 questions and then have 4 multiple choice answers where one is correct. The user is supposed to guess and then the amount correct at the end is displayed. My issue is randomly gathering the 5 questions from a list of 10 questions and then gathering the corresponding answers from the list of 10 answers. I am assigned to use a Question class and instances along the way, but I do not believe I am doing that entirely right. Any advice and examples are appreciated!

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)
Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
tm13
  • 21
  • 2
  • Use a dictionary to hold question:answer pairs. Select 5 random keys from the dictionary with the `random` module. – timgeb Nov 27 '17 at 13:51

0 Answers0