Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save/Loading using pickle
#1
So i am trying to make a game using python, and i want to save the amount of coins the player has after they earn coins using pickle, but i have no idea how to do this. let's say i got 10 coins, and i already have 59 coins, i want to write to the file that i have 69 coins, also i want to be able to print the amount of coins the player has when they input 'pls bal' and take the data from the pickle data save thingy and have that.
Larz60+ write Nov-17-2020, 07:55 PM:
Please don't change text color, which may not display properly in all themes.

Forum Colors have been carefully selected, and change to highlight various features.
Reply
#2
It is very easy. Here is a way
import pickle

def store(filename, **data):
    with open(filename, 'wb') as ofh:
        pickle.dump(data, ofh)
        
def fetch(filename):
    with open(filename, 'rb') as ifh:
        return pickle.load(ifh)


def main():
    filename = 'foo.pkl'
    store(filename, amount=59)
    data = fetch(filename)
    print(data)
    amount = data['amount']
    amount += 10
    store(filename, amount=amount)
    data = fetch(filename)
    print(data)
    
if __name__ == '__main__':
    main()
Output:
{'amount': 59} {'amount': 69}
Reply
#3
please can you point out what each line of code does? sorry quite new and am a bit confused
Reply
#4
Please post the code of the game to see if this applies to your game. The code that I showed stores a number in a file and retrieves that number from the file. Is it not what you want?
Reply
#5
(Nov-18-2020, 08:16 PM)Gribouillis Wrote: Please post the code of the game to see if this applies to your game. The code that I showed stores a number in a file and retrieves that number from the file. Is it not what you want?

it is, i just don't understand what does what in the code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 268 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,537 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,606 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,694 Aug-30-2021, 02:49 PM
Last Post: law
  computing entropy using pickle files baran01 2 2,374 Dec-30-2019, 09:45 PM
Last Post: micseydel
  Tkinter don't get ver from file via pickle storzo 2 2,517 Jul-31-2019, 03:50 PM
Last Post: storzo
  pickle docs say bytes in one place, strings in another Skaperen 2 2,106 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 10,855 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 2,898 Feb-15-2019, 04:39 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020