Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:34:44+00:00 2026-06-15T23:34:44+00:00

I have here some python 3 code that uses the pickle module. It is

  • 0

I have here some python 3 code that uses the “pickle” module. It is supposed to store high scores for a game. When I open the program again it instead gives me the default “A : 100…” high scores.

def __init__(self):
    self.filename = "highscores.dat"
    self.numScores = 5

    if not os.path.isfile(self.filename):
        self.file = open(self.filename, "wb")
        self.scores = [100 for i in range(self.numScores)]
        self.names = ["A", "B", "C", "D", "E"]
        self.highscores = [(self.names[i], self.scores[i]) for i in range(self.numScores)]
        self.updateFile()
    else:
        file = open(self.filename, "rb")
        self.highscores = pickle.load(file)
        file.close()
        self.file = open(self.filename, "wb")

        self.names = [highscore[0] for highscore in self.highscores]
        self.scores = [highscore[1] for highscore in self.highscores]

 def addScore(self, name, score):
    self.scores.append(score) #Add new score 
    self.scores.sort(reverse = True) #Sort
    self.names.insert(self.scores.index(score), name)
    self.names = self.names[:self.numScores] # Top 5
    self.scores = self.scores[:self.numScores]
    self.highscores = [(self.names[i], self.scores[i]) for i in range(self.numScores)]
    self.updateFile()

def updateFile(self):
    pickle.dump(self.highscores, self.file)

This is only the parts of the code where I believe the problem to reside. I will post more if it is needed. I would be happy to answer your questions. thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:34:45+00:00Added an answer on June 15, 2026 at 11:34 pm

    You’ll need to re-open the file for writing each time. Currently, you are writing new records each time the score changes, all one after the other, in your file. Your file now contains several pickles but only the first is being read.

    Change your code to:

    def __init__(self):
        self.filename = "highscores.dat"
        self.numScores = 5
    
        if not os.path.isfile(self.filename):
            self.scores = [100 for i in range(self.numScores)]
            self.names = ["A", "B", "C", "D", "E"]
            self.highscores = [(self.names[i], self.scores[i]) for i in range(self.numScores)]
        else:
            with open(self.filename, "rb") as f:
                self.highscores = pickle.load(f)
            self.names = [highscore[0] for highscore in self.highscores]
            self.scores = [highscore[1] for highscore in self.highscores]
    
    def updateFile(self):
        with open(self.filename, 'wb') as f:
            pickle.dump(self.highscores, f)
    

    with addScore unchanged.

    The highscore file is now being written from scratch each time the score changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code here that uses bitsets to store many 1 bit values
I am writing some code that uses the Element.find() method from Python's xml.etree.ElementTree module.
I'm trying to test some python code that uses urllib2 and lxml. I've seen
I'm writing some python code to interact with a C DLL that uses structures
I have some Python code that's receiving a string with bad unicode in it.
I have some python code for Google App Engine that responds with the string
I have some Python / Numpy code that is running slow and I think
I have some python code that fails: import sys print (MathCheats Times-Ed by jtl999)
I have some Python code that's dependent upon passing around some lambdas, and they
I have some python code that's throwing a KeyError exception. So far I haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.