简体   繁体   English

TypeError:字符串索引必须是整数

[英]TypeError: string indices must be integers

So, I was trying to make code for a Pokédex, and I have the following section of code set to print out the stats of a Pokémon. 因此,我试图为神奇宝贝编写代码,并且将下面的代码部分设置为打印出神奇宝贝的统计信息。 Note that there are 715 other dictionaries before these three: 请注意,这三个词典之前还有715个其他词典:

Xerneas = {'HP': 126, 'Atk': 131, 'Def': 95, 'SAtk': 131, 'SDef': 98, 'Spd': 99,
           'Ability': "Fairy Aura", 'Type': "Fairy"}
Yveltal = {'HP': 126, 'Atk': 131, 'Def': 95, 'SAtk': 131, 'SDef': 98, 'Spd': 99,
           'Ability': "Dark Aura", 'Type': "Dark/Flying"}
Zygarde = {'HP': 108, 'Atk': 100, 'Def': 121, 'SAtk': 81, 'SDef': 95, 'Spd': 95,
           'Ability': "Aura Break", 'Type': "Dragon/Ground"}

Mode = input('Which mode? ')
if Mode == "Pokedex":
    Pokemon = input("Which Pokemon? ")
    print(Pokemon['HP'],
          Pokemon['Atk'],
          Pokemon['Def'],
          Pokemon['SAtk'],
          Pokemon['SDef'],
          Pokemon['Spd'],
          Pokemon['Ability'],
          Pokemon['Type'])

So then I tried running my code and calling Zygarde's information, but the following error came up: 因此,我尝试运行代码并调用Zygarde的信息,但是出现了以下错误:

Traceback (most recent call last):
  File "/Users/Cobalt/Documents/Python/Pokédex.py", line 154, in <module>
    print(Pokemon['HP'],
TypeError: string indices must be integers
>>> 

Is there an easy solution to this problem? 有解决这个问题的简便方法吗? I'm willing to reformat 718 dictionaries if necessary. 如有必要,我愿意重新格式化718个词典。

By the way, this is my first question on this website, so please don't be too harsh with me. 顺便说一句,这是我在本网站上的第一个问题,所以请不要对我太苛刻。

You need to create a multidimension array like so 您需要像这样创建一个多维数组

pokemon = { 'Xerneas':  {'HP': 126, 'Atk': 131, 'Def': 95, 'SAtk': 131, 'SDef': 98, 'Spd': 99,
           'Ability': "Fairy Aura", 'Type': "Fairy"},
  'Yveltal': {'HP': 126, 'Atk': 131, 'Def': 95, 'SAtk': 131, 'SDef': 98, 'Spd': 99,
           'Ability': "Dark Aura", 'Type': "Dark/Flying"},
  'Zygarde': {'HP': 108, 'Atk': 100, 'Def': 121, 'SAtk': 81, 'SDef': 95, 'Spd': 95,
           'Ability': "Aura Break", 'Type': "Dragon/Ground"}
}

Then when you are asking for which pokemon to use: 然后,当您询问要使用哪种神奇宝贝时:

    pokemon_type = input("Which Pokemon? ")
    print(pokemon[pokemon_type]['HP'],
          pokemon[pokemon_type]['Atk'],
          pokemon[pokemon_type]['Def'],
          pokemon[pokemon_type]['SAtk'],
          pokemon[pokemon_type]['SDef'],
          pokemon[pokemon_type]['Spd'],
          pokemon[pokemon_type]['Ability'],
          pokemon[pokemon_type]['Type'])
Pokemon = input("Which Pokemon? ")

This makes Pokemon a string. 这使Pokemon成为一个字符串。 You then attempt to access this string by doing: 然后,您尝试通过执行以下操作访问此字符串:

Pokemon['HP']

Now the interpreter complains because HP isn't a valid index in the context of a string. 现在,解释器抱怨是因为HP在字符串上下文中不是有效索引。 I'm guessing you meant to have a dictionary here? 我猜你打算在这儿放字典吗?

What you probably want to do is something along the lines of this: 您可能想要做的事情与此类似:

Make a dictionary containing all the pokemon, for example we could call it pokemon_dict 制作一个包含所有神奇宝贝的字典,例如,我们可以称它为pokemon_dict

pokemon_dict = { "xerneas" : Xerneas, .....}

selection = input("Which Pokemon? ")
pokemon_selected = pokemon_dict[selection]
print(pokemon_selected['HP'])

This statement 这个说法

Pokemon = input("Which Pokemon? ")

sets Pokemon to a string. Pokemon设置为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM