python - 为什么我不能在 pPython 的类中腌制嵌套的字典或列表?
问题描述
我目前正在制作基于文本的游戏,但是当我尝试腌制我的玩家类时,它会保存除字典和列表之外的所有内容。这是我保存游戏的代码:
# Saving
def save():
os.system('cls')
print("As", player.Name, "opens the book, series of names and stats are written throughout the pages...")
ask = input("Write your name and stats in the book?\n[" + Fore.GREEN + "Y" + Fore.RESET + "]Yes "
"\n[" + Fore.GREEN + "N" + Fore.RESET + "]No\n> ").upper()
if ask == "Y" or ask == "YES":
print("(Which Save File will you save into?)")
print("[" + Fore.GREEN + "1" + Fore.RESET + "]Save_File1")
print("[" + Fore.GREEN + "2" + Fore.RESET + "]Save_File2")
print("[" + Fore.GREEN + "3" + Fore.RESET + "]Save_File3")
print("[" + Fore.GREEN + "4" + Fore.RESET + "]Save_File4")
save_choice = input('> ')
print()
if save_choice == '1':
with open("Save_File1.pkl", "wb") as save_file:
pickle.dump(player, save_file) # Creates the file and puts the data into the file
elif save_choice == '2':
pickle.dump(player, open('Save_File2.pkl', 'wb')) # Creates the file and puts the data into the file
elif save_choice == '3':
pickle.dump(player, open('Save_File3.pkl', 'wb')) # Creates the file and puts the data into the file
elif save_choice == '4':
pickle.dump(player, open('Save_File4.pkl', 'wb'))
else:
print('Invalid Command')
input('> ')
save()
print("Your progress has been saved!")
input("> ")
这是我加载游戏的代码:
# Loading
def load():
while True:
os.system('cls')
print("( Load a save file )")
print("[" + Fore.GREEN + "1" + Fore.RESET + "]Save_File1")
print("[" + Fore.GREEN + "2" + Fore.RESET + "]Save_File2")
print("[" + Fore.GREEN + "3" + Fore.RESET + "]Save_File3")
print("[" + Fore.GREEN + "4" + Fore.RESET + "]Save_File4")
load_choice = input('> ')
global player
if load_choice == '1':
with open("Save_File1.pkl", "rb") as save_file:
player = pickle.load(save_file)
elif load_choice == '2':
player = pickle.load(open('Save_File2.pkl', 'rb')) # Loads the file
elif load_choice == '3':
player = pickle.load(open('Save_File3.pkl', 'rb')) # Loads the file
elif load_choice == '4':
player = pickle.load(open('Save_File4.pkl', 'rb')) # Loads the File
以下是球员数据:
# Player Stats
class Player(object):
Name = ""
Class = ""
Race = ""
HP = 0
MaxHP = 0
SP = 0
MaxSP = 0
BP = 0
MaxBP = 100
Power = 0
Vitality = 0
Dexterity = 0
Intelligence = 0
Perception = 0
Persona = 0
XP = 0
MXP = 50
Gold = 50
Lv = 1
Offence = 0
Defence = 0
Base_Offence = 0
Base_Defence = 0
Offence_Mod = 0
Defence_Mod = 0
Bounty = 0
Reputation = {"Commoner": 0, "Noble": 0, "Peasant": 0, "Adventurer": 0}
events = []
Lang = ["Common"] # This one doesn't Save
Skills = {} # This one doesn't Save
Status_Effects = []
Key_Items = []
Health_Potion = 0
MaxHealth_Potion = 5
Ether_Potion = 0
MaxEther_Potion = 5
Antidote_Potion = 0
MaxAntidote_Potion = 5
location = ""
weapon = ""
armr = ""
player = Player()
我谷歌没有任何帮助,所以我希望我能在这里得到帮助。任何帮助将不胜感激!
编辑:想通了!我使用的是类变量而不是实例变量!我将播放器类切换为:
# Player Stats
class Player():
def __init__(self):
self.Name = ""
self.Class = ""
self.Race = ""
self.HP = 0
self.MaxHP = 0
self.SP = 0
self.MaxSP = 0
self.BP = 0
self.MaxBP = 100
self.Power = 0
self.Vitality = 0
self.Dexterity = 0
self.Intelligence = 0
self.Perception = 0
self.Persona = 0
self.XP = 0
self.MXP = 50
self.Gold = 50
self.Lv = 1
self.Offence = 0
self.Defence = 0
self.Base_Offence = 0
self.Base_Defence = 0
self.Offence_Mod = 0
self.Defence_Mod = 0
self.Bounty = 0
self.Reputation = {"Commoner": 0, "Noble": 0, "Peasant": 0, "Adventurer": 0}
self.events = []
self.Lang = ["Common"]
self.Skills = {}
self.Status_Effects = []
self.Key_Items = []
self.Health_Potion = 0
self.MaxHealth_Potion = 5
self.Ether_Potion = 0
self.MaxEther_Potion = 5
self.Antidote_Potion = 0
self.MaxAntidote_Potion = 5
self.location = ""
self.weapon = ""
self.armr = ""
player = Player()
我对编程有点陌生,所以这对我很有帮助......
解决方案
您制作了类变量,而不是实例变量。您要保存的数据都不是实例的一部分,因此不会保存。(确实被保存的部分也不会被保存,除非您后来在您未发布的代码中无意中用新的实例变量遮蔽了类变量而没有意识到这一点。)
使用实例变量。
推荐阅读
- c++ - E2108 使用 GetValue 路径评估器时 typedef 'TJSONObject' 错误使用不当
- python - 使用 Python Selenium,在 Pinterest.com 上找不到元素,但我可以在 Firefox 中使用检查器看到它
- java - 为什么我无法使用 Apache CLI 解析选项
- sql - Tsql 值在 json 数组中(任何索引)
- java - 如何获取 Java SWT 表中列的值
- excel - 返回表格excel中的给定值
- angular-material2 - 如何从角材料对话框中删除填充?
- azure - Azure Functions 中的 System.Data.SqlClient 4.6.0
- android - 在列表视图中显示每个线程进度的进度条
- sql - 完全外连接问题 - 几个月没有收入