python: def typehelper(poke_name): """ Question 5 - API Now that you've acquired a new helper, you want to take care of them! Use the provided API to find the type(s) of the Pokemon whose name is given. Then, for each type of the Pokemon, map the name of the type to a list of all types that do double damage to that type. Note: Each type should be considered individually. Base URL: https://pokeapi.co/ Endpoint: api/v2/pokemon/{poke_name} You will also need to use a link provided in the API response. Args: Pokemon name (str) Returns: Dictionary of types Hint: You will have to run requests.get() multiple times! >>> typehelper("bulbasaur") {'grass': ['flying', 'poison', 'bug', 'fire', 'ice'], 'poison': ['ground', 'psychic']} >>> typehelper("corviknight") {'flying': ['rock', 'electric', 'ice'], 'steel': ['fighting', 'ground', 'fire']} """ # pprint(typehelper("bulbasaur")) # pprint(typehelper("corviknight"))
python:
def typehelper(poke_name):
"""
Question 5 - API
Now that you've acquired a new helper, you want to take care of them!
Use the provided API to find the type(s) of the Pokemon whose name is
given. Then, for each type of the Pokemon, map the name of the type to
a list of all types that do double damage to that type.
Note: Each type should be considered individually.
Base URL: https://pokeapi.co/
Endpoint: api/v2/pokemon/{poke_name}
You will also need to use a link provided in the API response.
Args:
Pokemon name (str)
Returns:
Dictionary of types
Hint: You will have to run requests.get() multiple times!
>>> typehelper("bulbasaur")
{'grass': ['flying', 'poison', 'bug', 'fire', 'ice'], 'poison': ['ground', 'psychic']}
>>> typehelper("corviknight")
{'flying': ['rock', 'electric', 'ice'], 'steel': ['fighting', 'ground', 'fire']}
"""
# pprint(typehelper("bulbasaur"))
# pprint(typehelper("corviknight"))
To help you take care of your new helper, this code can be used to find the type(s) of a Pokemon whose name is given. It uses the provided API to send a request to the given endpoint and retrieves the type of the Pokemon. It then maps the name of the type to a list of all types that do double damage to that type.
The code starts by importing the `requests` library, which enables the sending and retrieval of data from the API. The `pprint` library is also imported to print the output in a readable format.
Then, a function called `typehelper` is defined that takes in the name of the Pokemon as an argument. It starts by building the base URL and endpoint with the Pokemon's name. Then, a `GET` request is sent to the API, and the response is stored in a variable as a dictionary.
To iterate over the Pokemon's types and map them to the list of all types that do double damage to them, a `for` loop is used. It runs through the dictionary, and for each type, it again sends a `GET` request to the API, retrieves the double damage list and stores it in a new variable.
Finally, the `pprint` library is used to print the output in a readable format. This code can be used to take care of your helper and check out the types of Pokemon.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps