Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- You are going to work to modify the Trivia Challenge game. You will need to mo

ID: 3709383 • Letter: #

Question

- You are going to work to modify the Trivia Challenge game. You will need to modify the game so that each question has a unique point value associated with it. The player's score should be the total of all the point values answered correctly. You will also transform the high score system so that it maintains the scores list in a file.

- The assignment should begin with the Trivia Challenge game from the textbook. You should have this working prior to attempting to modify for the instructions in the assignment. You are encouraged to download the working code from the course documents folder to compare to your efforts to build the code in the book.

- Your submission MUST have variable points for each question, and they should be in the .txt file that the questions are located within. Think about scalability – in other words if we wanted to expand this game to 20 questions, all that you should have to alter is the .txt file that the questions are located in.

- Your other challenge is to maintain a high score list and that MUST read and write to a file. I suggested in another announcement/email that it was probably easier to write that high scores to another text or data file. If you do so, that file MUST be included in your submission.

*Below are the two files need for this assignment*

.py file

# Trivia Challenge

# Trivia game that reads a plain text file

import sys

def open_file(file_name, mode):
"""Open a file."""
try:
the_file = open(file_name, mode)
except IOError as e:
print("Unable to open the file", file_name, "Ending program. ", e)
input(" Press the enter key to exit.")
sys.exit()
else:
return the_file

def next_line(the_file):
"""Return next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", " ")
return line

def next_block(the_file):
"""Return the next block of data from the trivia file."""
category = next_line(the_file)
  
question = next_line(the_file)
  
answers = []
for i in range(4):
answers.append(next_line(the_file))
  
correct = next_line(the_file)
if correct:
correct = correct[0]
  
explanation = next_line(the_file)

return category, question, answers, correct, explanation

def welcome(title):
"""Welcome the player and get his/her name."""
print(" Welcome to Trivia Challenge! ")
print(" ", title, " ")

def main():
trivia_file = open_file("trivia.txt", "r")
title = next_line(trivia_file)
welcome(title)
score = 0

# get first block
category, question, answers, correct, explanation = next_block(trivia_file)
while category:
# ask a question
print(category)
print(question)
for i in range(4):
print(" ", i + 1, "-", answers[i])

# get answer
answer = input("What's your answer?: ")

# check answer
if answer == correct:
print(" Right!", end=" ")
score += 1
else:
print(" Wrong.", end=" ")
print(explanation)
print("Score:", score, " ")

# get next block
category, question, answers, correct, explanation = next_block(trivia_file)

trivia_file.close()

print("That was the last question!")
print("You're final score is", score)

main()  
input(" Press the enter key to exit.")

.txt file

An Episode You Can't Refuse
On the Run With a Mammal
Let's say you turn state's evidence and need to "get on the lamb." If you wait /too long, what will happen?
You'll end up on the sheep
You'll end up on the cow
You'll end up on the goat
You'll end up on the emu
1
A lamb is just a young sheep.
The Godfather Will Get Down With You Now
Let's say you have an audience with the Godfather of Soul. How would it be /smart to address him?
Mr. Richard
Mr. Domino
Mr. Brown
Mr. Checker
3
James Brown is the Godfather of Soul.
That's Gonna Cost Ya
If you paid the Mob protection money in rupees, what business would you most /likely be insuring?
Your tulip farm in Holland
Your curry powder factory in India
Your vodka distillery in Russian
Your army knife warehouse in Switzerland
2
The Rupee is the standard monetary unit of India.
Keeping It the Family
If your mother's father's sister's son was in "The Family," how are you /related to the mob?
By your first cousin once removed
By your first cousin twice removed
By your second cousin once removed
By your second cousin twice removed
1
Your mother's father's sister is her aunt -- and her son is your /mother's first cousin. Since you and your mother are exactly one generation /apart, her first cousin is your first cousin once removed.
A Maid Man
If you were to literally launder your money, but didn't want the green in your /bills to run, what temperature should you use?
Hot
Warm
Tepid
Cold  
4
According to my detergent bottle, cold is best for colors that might run.

Explanation / Answer

def open_file(file_name, mode):

"""Open a file."""

try:

the_file = open(file_name, mode)

except(IOError), e:

print "Unable to open the file", file_name, "Ending program. ", e

raw_input(" Press the enter key to exit.")

sys.exit()

else:

return the_file

def next_line(the_file):

"""Return next line from the trivia file, formatted."""

line = the_file.readline()

line = line.replace("/", " ")

return line

def next_block(the_file):

"""Return the next block of data from the trivia file."""

category = next_line(the_file)

#print ("asdkjsadkaskjhdasjhkdjikhsdasd"+category ) # debugging code

point_value = 0

question = next_line(the_file)

  

answers = []

answers.append(next_line(the_file))

#print (answers[0]+".fdkandsijnasdasd95959595") # debugging code

if( answers[0]=="True "):

answers.append(next_line(the_file))

else:

for i in range(3):

answers.append(next_line(the_file))

  

correct = next_line(the_file)

if correct:

correct = correct[0]

#print(correct + ":!!!!") # debugging code

#print("--=-=-=-="+next_line(the_file).strip()) # debugging code

point_value = (int)(next_line(the_file).strip())

explanation = next_line(the_file)

return category, question, answers, correct, explanation, point_value

def welcome(title):

"""Welcome the player and get his/her name."""

print " Welcome to Trivia Challenge! "

print " ", title, " "

def main():

trivia_file = open_file("trivia2.txt", "r")

title = next_line(trivia_file)

welcome(title)

score = 0

# get first block

category, question, answers, correct, explanation, point_value = next_block(trivia_file)

while category:

# ask a question

print category

print question

i=0

for a in answers:

print " ", i + 1, "-", a

i = i + 1

# get answer

answer = raw_input("What's your answer? Type 'quit' to end the game: ").lower()

# check answer

if answer == "quit":

break

if answer == correct:

print " Right!",

print point_value

score += point_value

else:

print " Wrong.",

print explanation

print "Score:", score, " "

# get next block

category, question, answers, correct, explanation, point_value = next_block(trivia_file)

trivia_file.close()

print "That was the last question!"

print "You're final score is:", score

main()  

raw_input(" Press the enter key to exit.")