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.
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...
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...
[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.
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";    }    /**...
Using Python and Kivy to create a App to ask users 5 questions with Yes or...
Using Python and Kivy to create a App to ask users 5 questions with Yes or No button. 1. Are you older than 14? 2. Did you pass 8th or 9th Grade? 3. Are you ready gor highschool? 4. Do you have 4 more years to complete highschool? 5. Are you homeschool? If the users click Yes for 3 questions or more than you should get a results page saying this users is in highschool or else the user is...
Can you please solve these questions/ statements using python? I started with "importing" the file. I...
Can you please solve these questions/ statements using python? I started with "importing" the file. I only need question one to be answered not two-four. Can use whatever data frame of choice, I just need a sample code for each line. Thank you #1. #Fit a linear regression model on data: USA_housing.csv to predict the Price of the house. import pandas as pd housing_df = pd.read_csv("USA_Housing.csv") #Income: Avg. area income #Age: Avg age of the houses #Bedrooms: Avg No of...
Assignment Requirements Write a python application that consists of two .py files. One file is a...
Assignment Requirements Write a python application that consists of two .py files. One file is a module that contains functions used by the main program. NOTE: Please name your module file: asgn4_module.py The first function that is defined in the module is named is_field_blank and it receives a string and checks to see whether or not it is blank. If so, it returns True, if not it return false. The second function that is defined in the module is named...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT