Python I';我收到这个错误-->;TypeError:字符串索引必须是整数
我试图通过制作一个基于文本的rpg来学习面向对象编程,如下所示。其中与我的问题有关的部分有:Python I';我收到这个错误-->;TypeError:字符串索引必须是整数,python,python-3.x,oop,compiler-errors,Python,Python 3.x,Oop,Compiler Errors,我试图通过制作一个基于文本的rpg来学习面向对象编程,如下所示。其中与我的问题有关的部分有: def equipArmor(self): for armor in self.armorsOwned: select = 1 if self.armor == armor: print(str(select) + ". " + str(armor["Name"]) + " (Equipped)")
def equipArmor(self):
for armor in self.armorsOwned:
select = 1
if self.armor == armor:
print(str(select) + ". " + str(armor["Name"]) + " (Equipped)")
else:
print(str(select) + ". " + str(armor["Name"]))
select += 1
armor_choice = input("Type the name of the armor you would like to equip\n")
for i in self.armorsOwned:
if armor_choice == i["Name"]:
if self.armor == i:
print("You already have that equipped")
else:
self.armor = i["Name"]
print("You equipped the {}".format(i["Name"]))
self.maxhp += i["Effect"]
以及:
以下是其余部分,以便您了解我的代码的上下文:
import time
import sys
class Player:
def __init__(self):
self.level = 1
self.exp = 0
self.gold = 0
self.maxhp = 20
self.hp = self.maxhp
self.attack = 1
self.weapon = ""
self.armor = ""
self.weaponsOwned = {}
self.armorsOwned = {}
def checkHp(self):
self.hp = max(0, min(self.hp, self.maxhp))
def deadCheck(self):
if self.hp == 0:
print("You died!")
sys.exit()
def equipArmor(self):
for armor in self.armorsOwned:
select = 1
if self.armor == armor:
print(str(select) + ". " + str(armor["Name"]) + " (Equipped)")
else:
print(str(select) + ". " + str(armor["Name"]))
select += 1
armor_choice = input("Type the name of the armor you would like to equip\n")
for i in self.armorsOwned:
if armor_choice == i["Name"]:
if self.armor == i:
print("You already have that equipped")
else:
self.armor = i["Name"]
print("You equipped the {}".format(i["Name"]))
self.maxhp += i["Effect"]
class Enemy:
def __init__(self, attack, maxhp, exp, gold):
self.exp = exp
self.gold = gold
self.maxhp = maxhp
self.hp = maxhp
self.attack = attack
def checkHp(self):
self.hp = max(0, min(self.hp, self.maxhp))
def enemyDeadCheck(self):
if self.hp == 0:
return True
class Shop:
armors = {"BronzeArmor":{"Name": "Bronze armor",
"Cost": 30,
"Effect": 10},
"SilverArmor":{"Name": "Silver armor",
"Cost": 75,
"Effect": 20}}
character = Player()
character.armorsOwned.update(Shop.armors["BronzeArmor"])
character.equipArmor()
我要做的是打印出我所有的盔甲,如果装备了,在旁边打印“装备”,从输入中接收要装备的盔甲的名称,检查它是否已经装备,如果没有装备,再装备它。然而,标题中提到的错误阻止了我这样做。为什么会这样?字符串表示什么?
类型错误:字符串索引必须是整数
是Python中非常常见的错误,尤其是在开发过程中。它表示您的变量是字符串,但您正试图将其用作字典
例如:
x = {'Name': 'Bronze Armor'}
print(x['Name']) # Bronze Armor
x = 'Bronze Armor'
print(x['Name']) # raises TypeError: string indices must be integers
查看错误的堆栈跟踪,它会告诉您是在哪一行出错的。在字典上循环(例如,for i in self.armorsOwned
)返回一个键的iterable,而不是条目。因此,i
被设置为键字符串,而不是armor字典
您希望将字典上的所有循环转换为以下内容:
对于self.armorsOwned.values()中的i:
没有足够的分数进行评论,因此将其作为答案发布。
当您将列表视为字典时,通常会引发此错误
如果您可以包括显示错误的#线,这样我就可以锁定该点,这会很有帮助,但对我来说,这看起来很可疑:
self.armorsOwned = {}
如果这只是一本装甲词典。在这种情况下,您可以这样提取装甲名称:
if self.armor == armor:
print(str(select) + ". " + str(self.armorsOwned[armor]["Name"]) + " (Equipped)")
else:
print(str(select) + ". " + str(self.armorsOwned[armor]["Name"]))
在执行任何字符串操作之前,您还可以尝试打印这些变量的值,以查看它们包含的内容:
def equipArmor(self):
print(self.armorsOwned)
for armor in self.armorsOwned:
print(armor)
select = 1
错误消息不仅仅是
somethinger错误:这里有几个词。还有一个堆栈跟踪,其中充满了关于问题发生的位置和方式的有用信息。不要删除堆栈跟踪!我试过了,但错误仍然存在。是否还有其他我可能做错的事情?附加堆栈跟踪(错误日志)和问题。回溯(最后一次调用):文件“C:\Users\mohdi\Downloads\Python\Python OOP RPG.py”,第72行,在character.equipArmor()文件“C:\Users\mohdi\Downloads\Python\Python OOP RPG.py”,第33行,在equipArmor打印(str(select)+“+str(armor[“Name]”)中)TypeError:字符串索引必须是整数我认为armor或self.armorsOwned在此阶段为空/未初始化。
def equipArmor(self):
print(self.armorsOwned)
for armor in self.armorsOwned:
print(armor)
select = 1