# By Heidi Newton from random import randrange quiz_questions= [] quiz_answers = [] #This is the highest level method, that controls the overall game. #It loops around printing random quiz questions for the user to answer. def run_game(): read_quiz_into_lists() print_intro() while True: print("################################") question_id = randrange(0, len(quiz_questions)) do_question(question_id) print("################################") def print_intro(): print("Welcome to the great quiz game!") print("You will be presented with a series of multiple choice questions") print("Enter the letter corresponding to the answer you think is right") #print("You can quit at any time by entering 'Q' instead of an answer letter\n") #This method reads the quiz into the lists. def read_quiz_into_lists(): global quiz_questions, quiz_answers questions_file = open("questions.txt", "r") while True : next_question_line = questions_file.readline().rstrip() if not next_question_line : break quiz_questions.append(next_question_line) next_answer_line = questions_file.readline().rstrip() quiz_answers.append(next_answer_line) #This method processes one question #It has 3 support methods (though it could be done as just one method) def do_question(question_id): display_question(question_id) user_answer = get_answer_from_user() check_answer(question_id, user_answer) #Displays the question specified by the ID number def display_question(question_id): print(quiz_questions[question_id] + "\n") def get_answer_from_user(): answer_from_user = input("Answer: ") return answer_from_user #Checks whether or not the user answer is the answer for this question #prints a message to tell the user whether or not it is #If it is not, it prints what the correct answer was. def check_answer(question_id, user_answer): global quiz_answers if quiz_answers[question_id] == user_answer: print("Congratulations, you are correct!") else: print("Sorry, but the answer was " + quiz_answers[question_id]) run_game()