In: Computer Science
Python:
Trivia Questionaire Create a trivia application
using the provided file which contains 15 questions in it.
Your application should randomly select how many questions will be
presented to the user and it must be a number not greater than the
number of questions in the file.
Every question that is displayed should be randomly selected from
the list and should be non-repetitive; in other words, you should
not ask the same question twice.
Your application should keep track of how many questions are
answered correctly in order to calculate the final score.
At the end of the program your program should provide the user with
feedback similar to the one below:
You answered 10 / 10 correctly.
Your score is a 100 / 100.
Python Code:
""" Python program that plays Trivia Questionaire """
import random
# Declaring variables
questions = []
answers = []
flag = 1
# Opening file
with open("d:\\Python\\Quiz.txt", "r") as fp:
# Reading questions from file
for line in fp:
# Stripping new line
line = line.strip()
# Checking for question or
answer
if flag == 1:
questions.append(line)
flag = 0
else:
answers.append(line)
flag = 1
# Checking number of questions
numQuestions = len(questions)
# Converting to dictionary
quizDict = {questions[i]: answers[i] for i in
range(len(questions))}
# Randomly selecting number of questions
quizQuestions = random.randint(1, numQuestions);
# Shuffling questions
random.shuffle(questions)
# Set Score to 0
score = 0
# Playing game
for i in range(quizQuestions):
# Printing question
print("\nQuestion ", (i+1), ": ", questions[i])
# Reading answer
ans = input("Your Answer: ")
# Comparing answer
if ans.lower() ==
quizDict[questions[i]].lower():
# Updating score
score = score + 1
# Printing results
print("\n\nYou answered ", score, " /", quizQuestions,
"correctly.")
print("\nYour score is a ", (score*10), " /", (quizQuestions*10),
".\n\n")
______________________________________________________________________________________________________
Code Screenshot:
______________________________________________________________________________________________________
Sample Run:
_________________________________________________________________________________________________
Input File Considered:
Who is the first person to reach Mount Everest?
Edmund Hillary
Who is the first person to reach North Pole?
Robert Peary
Who is the first person to reach the South Pole?
Amundsen
Which is the first country to print book?
China
Which is the first country to issue paper currency?
China
Which is the first country to commence competitive examination in
civil services?
China
Who is the first President of the U.S.A.?
George Washington
Who is the first Prime Minister of Britain?
Robert Walpole
Which is the first country to win football World cup?
Uruguay
Which is the first country to prepare a constitution?
USA
Which is the first country to host NAM summit?
Belgrade
Which was the first country to send man to the moon?
USA
Which is the first country to host the modern Olympics ?
Greece
Who was the first person to land on the moon?
Neil Armstrong
Which was the first shuttle to go in space?
Columbia