This is another of my “Python Text Adventure Game” tutorials. I’ll show you a simple way to save data to a file and load it again.
First of all, you have to import the regular expression module re.
Now set some variables:
Now you need a class with the save and load functions:
`class Option: #The function to save your current data def save(self): #Open the file or create it if it does not exist self.f = open(‘saves.txt’, ‘w+’) #Put everything you want to save in a single variable and use two spaces as a separator #It would look like this: Username 5 Sword self.savegame = name + " " + weapon + " " + str(level) #Now write it into the file. this will overwrite any existing save self.f.write(self.savegame) #Close the file self.f.close()
Add this code to test everything:
`loop = 1