-
-
Save anonymous/612a0f48d9a5f64320f938fb388bdb8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Trivia chellange | |
#This game reads a plain text file and asks you questions | |
import sys, pickle | |
def open_file(file_name, mode): | |
"""Opens a file""" | |
try: | |
file = open(file_name, mode) | |
except IOError as e: | |
print("Unable to open the text file", file_name, "Ending program.\n",e) | |
input("\n\nPress the enter key to exit...") | |
sys.exit() | |
else: | |
return file | |
def ask_question(question): | |
answer = None | |
while answer not in ("y", "n"): | |
answer = input(question).lower() | |
return answer | |
def next_line(file): | |
"""Returns next line from the trivia file, formatted""" | |
line = file.readline() | |
line = line.replace("/", "\n") | |
return line | |
def next_block(file): | |
"""Returns next block of data from the rivia file""" | |
category = next_line(file) | |
question = next_line(file) | |
answers = [] | |
for i in range(4): | |
answers.append(next_line(file)) | |
correct = next_line(file) | |
if correct: | |
correct = correct[0] | |
explanation = next_line(file) | |
try: | |
points = int(next_line(file)) | |
except ValueError: | |
points = "" | |
return category, question, answers, correct, explanation, points | |
def welcome(title): | |
"""Welcomes the player and gets his/her name""" | |
print("\t\tWelcome to Trivia Challenge!\n") | |
print("\t\t",title,"\n") | |
def read_scores(filename): | |
try: | |
with open(filename, 'rb') as scores_file: | |
return pickle.load(scores_file) | |
except (EOFError, FileNotFoundError): | |
return [] | |
def update_scores(filename, name, score): | |
scores = read_scores(filename) | |
scores.append((name, score)) | |
with open(filename, 'wb') as scores_file: | |
pickle.dump(scores, scores_file) | |
return scores | |
def main(): | |
file = open_file("trivia.txt", "r") | |
title = next_line(file) | |
welcome(title) | |
score = 0 | |
name = input("\n\nWhat is your name?: ") | |
# getting the first block | |
category, question, answers, correct, explanation, points = next_block(file) | |
while category: | |
#ask question | |
print(category) | |
print(question) | |
for i in range(4): | |
print("\t", i+1, "-", answers[i]) | |
#getting the answer | |
answer = input("What's your answer?: ") | |
if answer == correct: | |
print("\nRight!", end = " ") | |
score+= points | |
else: | |
print("\nWrong.", end = " ") | |
print(explanation) | |
print("Score:", score, "\n\n") | |
category, question, answers, correct, explanation, points = next_block(file) | |
file.close() | |
scores = update_scores('scores.pickle', name, score) | |
print("That was the last question!") | |
print("You're final score is", score) | |
if ask_question("Would you like to see High Scores?: ") == "y": | |
for name, score in scores: | |
print(name, score) | |
main() | |
input("\n\nPress the enter key to exit...") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment