Question

In: Computer Science

Python: Trivia Questionaire Create a trivia application using the provided file which contains 15 questions in...

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.

Solutions

Expert Solution

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


Related Solutions

Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
Using Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
On Python Preview the provided sample file called studentdata.txt. It contains one line for each student...
On Python Preview the provided sample file called studentdata.txt. It contains one line for each student in an imaginary class. The student’s name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student. Using the text file studentdata.txt write a program that calculates the average grade for each student, and print out the student’s name along with their average grade with two decimal places.
IN PYTHON PLEASE Create a file lists.py that contains the following functions: sumOfOdd(intList) The parameter intList...
IN PYTHON PLEASE Create a file lists.py that contains the following functions: sumOfOdd(intList) The parameter intList is supposed to be a list of integers. The function returns the sum (addition) of the odd integers from intList, and leaves intList not modified For instance, given [1,2,3,4], the function returns 4. The function performs no I/O. productOfEven(intList) The parameter intList is supposed to be a list of integers. The function returns the product (multiplication) of the even integers from intList, and leaves...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic...
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic array Create a for loop that will print the array index the array, print out the index append an element to the array, print out the array insert an element into the array, print out the array pop an element from the array, print out the array remove an element from the array, print out the array reverse the order of the array, print...
Using Java create an application that will read a tab-delimited file that displays the MLB standings...
Using Java create an application that will read a tab-delimited file that displays the MLB standings Below: League AL East Tampa Bay 37 20 NY Yankees 32 24 Toronto 29 27 Baltimore 23 33 Boston 22 34 League AL Central Minnesota 35 22 Chi White Sox 34 23 Cleveland 33 24 Kansas City 23 33 Detroit 22 32 League AL West Oakland 34 21 Houston 28 28 LA Angels 26 31 Seattle 25 31 Texas 19 37 League NL East...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called main that does the following: Creates a directory called CyberSecurity-Notes in the current working directory Within the CyberSecurity-Notes directory, creates 24 sub-directories (sub-folders), called Week 1, Week 2, Week 3, and so on until up through Week 24 Within each week directory, create 3 sub-directories, called Day 1, Day 2, and Day 3 Bonus Challenge: Add a conditional statement to abort the script if...
Create a linked list of Poem objects using the Poem.java file provided. You may create Poem...
Create a linked list of Poem objects using the Poem.java file provided. You may create Poem objects by hard coding (minimum 4), reading from a file, or getting user input. Print the list using ListIterator. Here is Poem.Java: public class Poem {    private String title;    private String poet;    /**    * No arg constructor    */    public Poem()    {        title = "not set";        poet = "not set";    }    /**...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT