-2
I was creating a dictionary playing with python Pokemon in pycharm Comunity, but when I went to perform the dictionary access, I was returned an error message in the code when trying to print one of the values inside a key in the dictionary that in turn there are also other dictionaries inside:
print(f'O pokemon {pesquisa} é de estagio {pk["stage"]}')
Typeerror: string indices must be integers
In case to make the code start running you need to write Charmander I am beginner in python and I would like to know how to solve this, follow the code below:
pokemons = {
'Charmander': {
'stage': '1',
'tipo': 'fogo',
'evoluções': {
'charmilion': {
'stage': '2',
'tipo': 'fogo',
'evoluções': {
'charizard': {
'stage': '3',
'tipo': 'fogo/voador',
},
},
},
},
},
}
pesquisa = str(input('Qual pokemon gostaria de saber mais sobre? '))
if pesquisa in pokemons:
for pk, pv in pokemons[pesquisa].items():
print(f'O pokemon {pesquisa} é de estagio {pk["stage"]}')
else:
print('Este pokemon não existe')
I understood thank you very much, it helped a lot, but what about in case I want to print after that the evolutions? Type after printing this also print in the same way the information of the evolutions, for example 'The Pokemon Charmande is of stage 1' from below 'being its evolutions: Charmilion of stage 2'. How would I access this second dictionary and print out your keys?
– Kaique
@Kaique, check the update.
– Paulo Marques
thank you very much, helped me understand much more now, very grateful.
– Kaique
@Kaique, I’m glad you helped. If you think it’s relevant, mark the answer as a solution and vote positive. :)
– Paulo Marques