-
November 15th, 2011, 09:17 PM
#1
Python Pickles
I have avoided coming here to AO about this certain topic for a long time. Instead I have joined a forum specifically dedicated to the python language.
Its a good forum, I like it and have learnt alot from there already, but I like AO better and it always helps to get things from different perspectives.
I have been having alot of trouble building a highscores list for a game in python. My instructions are to use pickled objects to populate a high score list.
I have done this ... or thought that I have. Problem is no matter what I do I can only get the first score to print out.
I've been messing with this for about 3 weeks and really want to move on.
I have finally decided to come here to ask for some input.
Here is the full game code:
Code:
# Trivia Challenge
# Trivia game that reads a plain text file
# (added a scoring system to Trivia Challenge)
# added a pickle file to keep track of high scores
import sys, pickle
def user_name():
name = input("Hello, what is your name? ")
#score = 0
name = name.title()
#high_scores = {name: score}
return name
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.\n", e)
input("\n\nPress 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("/", "\n")
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)
try:
score = int(next_line(the_file))
#score = int(score)
except ValueError:
score = 0
return category, question, answers, correct, explanation, score
def welcome(title, name):
"""Welcome the player and get his/her name."""
print("\n\t\tWelcome to Trivia Challenge,", name, "!\n")
print("\t\tThis is", title, "\n")
def create_name_list(name):
name_list = [name]
#if name_list:
#name_list.append(name)
return name_list
def create_score_list(new_score):
score_list = [new_score]
#score_list.append(new_score)
return score_list
def store_scores(name, new_score):
f = open("pickledScores.dat", "rb+")
try:
y = pickle.load(f)
except EOFError:
f.close()
f = open("pickledScores.dat", "wb")
name_list = [name]
pickle.dump(name_list, f)
print('x')
else:
name_list = [name]
name_list.append(y)
pickle.dump(name_list, f)
f.close()
#z= pickle.load(f)
#y.append(name)
#z.append(new_score)
#pickle.dump(y, f)
#pickle.dump(z, f)
def print_scores(score_list):
f = open("pickledScores.dat", "rb")
#full_name_list = name_list.append(name_list)
#full_score_list = score_list.append(score_list)
#print(full_name_list, full_score_list)
y = pickle.load(f)
#name_list_length = len(y)
#for i in range(name_list_length):
#print("\t", i + 1, "-", y[i], "\t - \t", score_list[i])
print(y)
#print(z)
f.close()
def main():
name = user_name()
trivia_file = open_file("trivia2.txt", "r")
title = next_line(trivia_file)
welcome(title, name)
new_score = 0
# get first block
category, question, answers, correct, explanation, score = 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("\nRight!", end=" ")
print("\nYou earned", score, "points.")
new_score += score
print("Your new score is:", new_score, "\n\n")
else:
print("\nWrong.", end=" ")
print(explanation)
print("Your score is still:", new_score, "\n\n")
# get next block
category, question, answers, correct, explanation, score = next_block(trivia_file)
trivia_file.close()
print("That was the last question!")
print("You're final score is", new_score)
if new_score >=90:
print("\n\nCongratulations", name, "you've made the high score list!!")
name_list = create_name_list(name)
score_list = create_score_list(new_score)
store_scores(name, new_score)
print_scores(score_list)
else:
print("\n\nSorry, ", name, "but", new_score, "is not good enough to make the high score list.")
print("Use the explanations to your wrong answers and try again.")
main()
input("\n\nPress the enter key to exit.")
My problems, I believe, lie within these two functions:
Code:
def store_scores(name, new_score):
f = open("pickledScores.dat", "rb+")
try:
y = pickle.load(f)
except EOFError:
f.close()
f = open("pickledScores.dat", "wb")
name_list = [name]
pickle.dump(name_list, f)
print('x')
else:
name_list = [name]
name_list.append(y)
pickle.dump(name_list, f)
f.close()
#z= pickle.load(f)
#y.append(name)
#z.append(new_score)
#pickle.dump(y, f)
#pickle.dump(z, f)
def print_scores(score_list):
f = open("pickledScores.dat", "rb")
#full_name_list = name_list.append(name_list)
#full_score_list = score_list.append(score_list)
#print(full_name_list, full_score_list)
y = pickle.load(f)
#name_list_length = len(y)
#for i in range(name_list_length):
#print("\t", i + 1, "-", y[i], "\t - \t", score_list[i])
print(y)
#print(z)
f.close()
I just can't figure out where Im going wrong with the pickling and unpickling. I have deleted, commented out, copied, pasted, deleted and experimented with so many different things I've gotten lost in my own thought.
I want to learn this, I want to get this and I want to move on. What am I not getting?
BTW, if it matters its python 3.2
Blessings
F
"In most gardens they make the beds too soft - so that the flowers are always asleep" - Tiger Lily
Similar Threads
-
By HTRegz in forum General Computer Discussions
Replies: 0
Last Post: September 19th, 2006, 07:28 PM
-
By HTRegz in forum The Security Tutorials Forum
Replies: 12
Last Post: January 28th, 2006, 08:02 PM
-
By HTRegz in forum Other Tutorials Forum
Replies: 20
Last Post: September 10th, 2005, 07:28 AM
-
By PacketThirst in forum Other Tutorials Forum
Replies: 2
Last Post: August 14th, 2005, 07:16 PM
-
By [pHA]XeNoCiDe in forum AntiOnline's General Chit Chat
Replies: 11
Last Post: July 8th, 2002, 07:56 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|