1

I am trying to use the Poke api to create a quick version of a pokedex. I just want the user to input a pokemons name and return the name of the pokemon they choose, and other small details. I can get my code to print out the entire json file for the pokemon but not just specific info. I am using Python 3.

My current code looks like this:

import requests
import pprint


def main():
    poke_base_uri = "https://pokeapi.co/api/v2/pokemon/"
    poke_choice = "pidgey"
    pokeresponse = requests.get(f"{poke_base_uri}{poke_choice}/")

    # Decode the response
    poke = pokeresponse.json()
    pprint.pprint(poke)

    print("\nGreat Choice you chose:")
    for name in poke:
        name_info = poke.get(pokemon_species)
        print(name_info.json().get('name'))
vitaliis
  • 4,082
  • 5
  • 18
  • 40
  • `pokemon_species` not define. `poke` is a python `dict`. Access the `keys` and extract the desired `values`. The `for-loop` makes no sense. `poke['species']` is `{'name': 'pikachu', 'url': 'https://pokeapi.co/api/v2/pokemon-species/25/'}`. `poke['species']['name']` is `pikachu`. – Trenton McKinney Dec 17 '20 at 22:42
  • If your problem is with parsing JSON, why does almost all your code deal with other things like user input? A good [mre] is the shortest possible thing that causes the specific problem you need help with when run without changes; code not related to the problem should be factored out. – Charles Duffy Dec 17 '20 at 22:45
  • 1
    I thought the question was reasonable as-is. They're obviously new to programming and don't understand the difference between the objects they're dealing with enough to get the data out. I think asking them to hard code a serialized example to read from would mean they know enough to answer their own question. – LISTERINE Dec 17 '20 at 23:01
  • @LISTERINE This is probably the reason for the other close votes (including mine) [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/7758804). Asking a question on SO should always be the last step after exhausting all other resources. SO is not just about answering the question for an OP, but creating a repository of useful answers for other people with similar issues. Downvotes or closure can be for many reasons, but in particular should be because the question doesn't show any research, is unclear, or not useful to others. – Trenton McKinney Dec 18 '20 at 00:46
  • @arther shelby, You can improve your question be showing the output you get from the above code and showing the desired output. – Tom Aranda Dec 18 '20 at 03:13

1 Answers1

3

Looks like you're very close.

I'd start by removing the for loop. You don't need that for this.

poke does contain all the info you're looking for, but you need to change your argument to poke.get. If you print out poke.keys() it will show you all the keys the dict has. You should see something like this:

dict_keys(['abilities', 'base_experience', 'forms', 'game_indices', 'height', 'held_items', 'id', 'is_default', 'location_area_encounters', 'moves', 'name', 'order', 'species', 'sprites', 'stats', 'types', 'weight'])

I think what you're trying to do is:

>>> name_info = poke.get("species")
{'name': 'pidgey', 'url': 'https://pokeapi.co/api/v2/pokemon-species/16/'}

You also don't need any more calls to .json(), they aren't actually available on the name_info object. .json is an attribute of a requests response object (that you got when you called requests.get). It returns a python dictionary containing the requested data from the site. Because it returns a plain python dict, you can just access all it's keys and values with .get.

I'd suggest reading up on python dictionaries. They're a very powerful object and learning to use them well is crucial to writing nice python.

https://docs.python.org/3/library/stdtypes.html?highlight=dict#dict

LISTERINE
  • 923
  • 1
  • 9
  • 14
  • @arthur shelby, when you get enough reputation, I recommend upvoting this answer since you accepted it. Not required, but it is courteous to provide an upvote to answers that help you solve problems. – Tom Aranda Dec 18 '20 at 03:17