我有一个程序,随机生成10个RPG字符。我已经实现了将它们保存到characterFile.json
的功能,这是该文件中包含的内容:
[
{
"health": 100,
"name": "da fu en ",
"power": 30,
"sAttackPwr": 60,
"speed": 10,
"type": "Elf"
},
{
"health": 100,
"name": "tuk ar da ",
"power": 90,
"sAttackPwr": 40,
"speed": 50,
"type": "Dragon"
},
{
"health": 100,
"name": "tuk el low ",
"power": 30,
"sAttackPwr": 60,
"speed": 10,
"type": "Elf"
},
{
"health": 100,
"name": "ant en ant ",
"power": 90,
"sAttackPwr": 40,
"speed": 50,
"type": "Dragon"
},
{
"health": 100,
"name": "tuk en el ",
"power": 90,
"sAttackPwr": 40,
"speed": 50,
"type": "Dragon"
},
{
"health": 100,
"name": "ar da ar ",
"power": 90,
"sAttackPwr": 40,
"speed": 50,
"type": "Dragon"
},
{
"health": 100,
"name": "kar ing tuk ",
"power": 30,
"sAttackPwr": 60,
"speed": 10,
"type": "Elf"
},
{
"health": 100,
"name": "da cha ing ",
"power": 50,
"sAttackPwr": 70,
"speed": 30,
"type": "Wizard"
},
{
"health": 100,
"name": "da cha low ",
"power": 50,
"sAttackPwr": 70,
"speed": 30,
"type": "Wizard"
},
{
"health": 100,
"name": "da cha tuk ",
"power": 30,
"sAttackPwr": 60,
"speed": 10,
"type": "Elf"
}
]
(将其粘贴到SO中时的格式是错误的,但是在文件中的设置方式是正确的)
我希望能够将其读回程序,从该列表中创建对象。
到目前为止,这是我将文件读回程序的内容:
def openCharsToFile(gameChars):
print("Loading File...")
ans = input("Please ensure the file is called 'characterFile.json', then press [1]...")
while True:
if ans == "1":
with open("characterFile.json") as read_file:
data = json.load(read_file)
print(data)
for x in data:
gameChars = characterClass(data[x][name], data[x][type], data[x][health], data[x][power], data[x][sAttackPwr], data[x][speed])
break
characterClass
是我用来实例化对象的类:
# Base Class for creating an RPG character
class character:
# __init__ method, creates the name, type, health properties
def __init__(self, charName, charType, charHealth):
self.name = charName
self.type = charType
self.health = charHealth
class characterClass(character):
def __init__(self, charName, charType, charHealth, charPower, charSAttackPwr, charSpeed):
character.__init__(self, charName, charType, charHealth)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
运行此代码时,出现以下错误:
TypeError: list indices must be integers or slices, not dict
现在,我对这个错误的理解是,我试图在列表中查找索引,而是将整个字典发送给它,但它不能变成整数
为了使用循环将其读回我的程序,我缺少什么?
答案 0 :(得分:1)
Here, x
is the actual character dict, not an index:
for x in data:
Try something like this instead:
for character in data:
gameChars = characterClass(character['name'], character['type'], character['health'], character['power'], character['sAttackPwr'], character['speed'])
I also added missing quotes here, and I would recommend using a more descriptive name than data
. How about characters
? Python classes are normally named with CamelCase
, so Character
and CharacterClass
might be better names than character
and characterClass
. Python's official style guide中的字符串。
最后,我认为您不需要break
。
答案 1 :(得分:0)
x不是索引,而是字典本身。 For循环遍历从JSON File加载到数据中的每个条目。正确的代码行应为:
gameChars = characterClass(x['name'], x['type'], x['health'], x['power'], x['sAttackPwr'], x['speed'])