Question: PYTHON ASSIGNMENT - Improve the trivia game so that it maintains a high scores list in a file. The program should record the player's name

PYTHON ASSIGNMENT - "Improve the trivia game so that it maintains a high scores list in a file. The program should record the player's name and score if the player makes the list. Store the high scores using a shelve object."

I have already completed this question to store the high scores in a pickled object but I am confused about shelve object. Thank you in advance!

Original Code Provided Below ( it uses a 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("\t\tWelcome to Trivia Challenge! ") print("\t\t", title, " ") def main(): trivia_file = open_file("alix_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("\t", 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.")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!