hi, Can someone help me i would like to run this program 3 times or more and would like to append the cPickle file as a high score table keeping my top scores. Right now it only records the last score thanks.
# Trivia Challenge # Trivia game that reads a plain text file 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.\n", e raw_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) return category, question, answers, correct, explanation def welcome(title): """Welcome the player and get his/her name.""" print "\t\tWelcome to Trivia Challenge!\n" print "\t\t", title, "\n" def main(): trivia_file = open_file("trivia.txt", "r") title = next_line(trivia_file) welcome(title) score = 0 bonus = 0 tries = 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 = raw_input("What's your answer?: ") tries = tries + 1 # check answer if answer == correct: print "\nRight!", score += 1 if tries == 1: bonus = 5 elif tries == 2: bonus = bonus + 10 elif tries == 3: bonus = bonus + 20 elif tries == 4: bonus = bonus + 30 elif tries == 5: bonus = bonus + 40 else: bonus = bonus print "\nWrong.", print explanation print "Score:", score, "\n\n" # get next block category, question, answers, correct, explanation = next_block(trivia_file) trivia_file.close() print "That was the last question!" print "You're score is:", score, "and your bonus", bonus total = score + bonus print "for a grand total ", total import cPickle, shelve name = raw_input("what is your name") High_Score = [name, total] pickle_file = open("pickles5.dat", "w") cPickle.dump(High_Score, pickle_file) pickle_file.close() # to read the pickle_file pickle_file = open("pickles5.dat", "r") High_Score = cPickle.load(pickle_file) print High_Score, "High Scores" main() raw_input("\n\nPress the enter key to exit.")
-- http://mail.python.org/mailman/listinfo/python-list