Ask coding questions

← Back to all posts
How do I make a save function in Python?
TortillaCook (4)

I'm working on a TBAG (Text-Based Adventure Game) and want to make it very long, and I feel as though a save function would be immensely helpful. I found some results on Stack Overflow, but none were something I could easily integrate. Feel free to take a peek inside my code and see what I have going on. There are currently no errors, but I want a block of code I can paste into a separate file ( and replace some variable names, and be done. Anyone know what to do?

tristans2 (0)

I think you should use a class that can do the intro, pause, and play functions you should add some variables to it like "self.location = 'house'" then have different sections in each location maybe, this way you can also add a save and load function that can read and write to a text file or something. you should also make different functions for each room so that it will be easier to expand on your game (also to under stand the code)

TortillaCook (4)

@tristans2 I tried making different functions for each room, but the variables wouldn't transfer between functions. I tried using global, but that didn't work either. I ended up just doing a bunch of while loops, and that fixed it so that you could move backwards in the game.

tristans2 (0)

@TortillaCook you could try doing something like this:

class Game():
  def __init__(self):
    # all of the variables you need
  def kitchen(self, inp):
    # all things kitchen related
  def run(self):
    while True:
      inp = input("some text")
      if == "kitchen":
InvisibleOne (2686)

Well, there's a couple of different ways and it depends on how complex you want to be. The first and easiest option would be to save data in a dictionary and store that dictionary inside of replit db, something like this:

from replit import db

db['save'] = {'name' : 'yada yada', 'level' : 'something or other'}

# then get that

player_data = db['save']

Or you could convert the file to a JSON and write it to a text file

import json

data = {
  'name' : 'John Doe',
  'level' : 3,

with open('save.txt', 'w') as file:

# then to restore it

with open('save.txt', 'r') as file:
  t =
  player_data = json.loads(t)

An even more complicated but technically better way to save the data would be to save it in a database such as MongoDB or an SQL database.

TortillaCook (4)

@InvisibleOne Ok... that sounds good, but I'm still confused on the data part. Would that just be whatever, and it would be changed? The "John Doe" and 3 would just be placeholders?

InvisibleOne (2686)

Yes, those are just placeholders in the dictionary, the dictionary is just a variable so it can be called whatever you want it to be. The items on the left inside of the dictionary are the key, and the items on the right are the value of that key. To get a value from a dictionary we use dictname[keyname] which will give you the value. Here's some examples

myDict = {
  "key1" : "This is the value of key1",
  "greeting" : "Hello there!",
  "number" : 5,
print(myDict['greeting']) # Hello there! Will be printed to the console
print(myDict['number']) # prints 5

myDict['number'] = 10

print(myDict['number']) # Now it will print 10 as the number. 


figglediggle (139)

What you want to do is specifically import the functions from the other file. For example

from TortillaChips import intro, play, pause

TortillaCook (4)

@figglediggle Well, I can move the functions, but I want to save the variables, so that when you close out of the game, you can come back, load the file, and keep going where you left off.

figglediggle (139)

@TortillaCook for that you will want to check out the database

Wumi4 (486)

Try save them as a JSON file, then when the user need to continue their progress, just load it.

TortillaCook (4)

@Wumi4 Sorry, should've mentioned that I'm relatively new, so... how do I do that?

Wumi4 (486)

@TortillaCook You can search for json in Python

TortillaCook (4)

I've been dabbling with "pickle," but haven't gotten anywhere.