I'm writing a game in Python that uses a save file feature. I've written a function that saves all the necessary variables, strings, lists and dictionaries to a file. All in all, there are 11 things to import when I call a function called "load()" which I wrote.
Here is load():
def load():
importedData = open("saveGame.dat", "r+").readlines()
for x in range(0,len(importedData)):
position = importedData[0]
health = importedData[1]
strength = importedData[2]
exp = importedData[3]
playerLevel = importedData[4]
# playerSpells = []
# spellsFromSaveFile = importedData[5]
# This doesn't work!
playerClass = importedData[6]
seenDialogues = importedData[7]
rank = importedData[8]
playerItems = importedData[9]
itemDesc = importedData[10]
specialItems = importedData[11]
return position, health, strength, exp, playerLevel, playerSpells, playerClass, seenDialogues, rank, playerItems, itemDesc, specialItems
And here is what the save file looks like:
2
24
4
96
1
['Blink']
None
2
Frajan
[]
{}
{}
Most of the dictionaries and lists will be blank, as they are used for items that are picked up later in the game (this save file was created early on in the game).
While I need to import a couple of lists and dictionaries, I'll focus on one of them. The list playerSpells
is saved as ['Blink']
in the save file, "saveGame.dat".
Here's what I've tried:
I've imported the list into a list called playerSpells
:
playerSpells = []
playerSpells = importedData[5]
print(playerSpells)
The line print(playerSpells)
returns this:
['Blink']
This seems like it worked, so I added the following loop:
playerSpells = []
playerSpells = importedData[5]
for x in range(0,len(playerSpells)):
print(playerSpells[x])
But got this:
[
'
B
l
i
n
k
'
]
So, my question is: How would I go about importing a list, from a file, into another list - but adding words to the list, not single characters?
Any help appreciated.