[Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2

Zack Hasanov zack.hasanov at gmail.com
Mon Aug 12 02:52:34 CEST 2013


Hello,

 

I am a python newbie.  I am reading this book (Python Programming for the
Absolute Beginner).  I am on Chapter 7, Question 2.  

 

"Improve the Trivia Challenge game so that it maintains a high-scores list
in a file. The program should record the player's name and score. Store the
high scores using a pickled object."

I have the following code so far:

 

def high_score():

    """Records a player's score"""

 

    high_scores = []

 

    #add a score // Do current stuff for adding a new score...

    name = input("What is your name? ")

    player_score = int(input("What is your score? "))

    entry = (name, player_score)

    high_scores.append(entry)

    high_scores.sort(reverse=True)

    high_scores = high_scores[:5]       # keep only top five

 

    # dump scores

    f = open("pickles1.dat", "wb")

    pickle.dump(high_scores, f)

    f.close()

 

    f = open("pickles1.dat", "rb")

    high_scores = pickle.load(f)

    print(high_scores)

    f.close()

 

When I execute this program in the main() program I get only the existing
single name, player_score list combination stored in the pickles1.dat file.

 

Can someone walk me through how it can store all the values each time the
program is ran?

 

Thanks,

Zack H.

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130811/58082c45/attachment.html>


More information about the Tutor mailing list