我试图在python3.5.1中为一个基于文本的游戏编写一个save/load函数,我在这个站点上找到了一些有用的代码,不幸的是,这两个函数都只需要一个文件路径作为参数之一。我正在学习python,对这个站点和一般的编码都是比较陌生的,所以请帮助我输入一个有效的文件路径(以及在这个例子中什么是有效的文件路径!)将不胜感激(再一次,我像乔恩·斯诺)。相关方法如下:def __init__(self, name):
self.name = name
self.maxHealth = 100
self.health = self.maxHealth
self.baseAttack = 10
self.credits = 10
self.augs = 0
self.weap = ["Basic Nanite Pack"]
self.currWeap = ["Basic Nanite Pack"]
global playerFile
playerFile = {"name":self.name, "health":self.maxHealth, "maxHealth":self.health, "baseAttack":self.baseAttack,
"weapon":self.weap, "currWeap":self.currWeap, "credits":self.credits, "augs":self.augs}
def saveGame(playerFile, filepath):
with open(filepath, 'w') as outfile:
for name,num in playerFile.items():
outfile.write("{};{};\n".format(num, name))
import csv
def loadGame(filepath):
with open(filepath) as infile:
# purposeful misspell for variable name
playerFil = dict((v,k) for v,k,_ in csv.reader(infile, delimiter=';'))
return playerFil