Skip to content

Commit 4270abf

Browse files
author
restevean
committed
#20 - Python
1 parent fd50537 commit 4270abf

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Exercise
3+
"""
4+
import requests
5+
6+
7+
def request_url(url):
8+
# Realizar la petición GET
9+
response = requests.get(url)
10+
11+
# Verificar si la petición fue exitosa
12+
if response.status_code == 200:
13+
return "Petición exitosa."
14+
else:
15+
return f"Error en la petición: {response.status_code},"
16+
17+
18+
"""
19+
Extra
20+
"""
21+
22+
23+
def get_pokemon_data(pokemon_identifier):
24+
base_url = "https://pokeapi.co/api/v2/pokemon/"
25+
url = f"{base_url}{pokemon_identifier.lower()}"
26+
27+
response = requests.get(url)
28+
29+
if response.status_code != 200:
30+
print(
31+
f"Error: No se pudo obtener información para {pokemon_identifier}. Código de estado: {response.status_code}")
32+
return
33+
34+
data = response.json()
35+
print(f"Name: {data['name'].capitalize()}")
36+
print(f"ID: {data['id']}")
37+
print(f"Weight: {data['weight']/10} kg")
38+
print(f"Height: {data['height']*10} cm")
39+
40+
types = [type_info['type']['name'] for type_info in data['types']]
41+
print(f"Tipe(s): {', '.join(types).capitalize()}")
42+
43+
# Obtener cadena de evoluciones
44+
species_url = data['species']['url']
45+
species_response = requests.get(species_url)
46+
species_data = species_response.json()
47+
evolution_chain_url = species_data['evolution_chain']['url']
48+
49+
evolution_chain_response = requests.get(evolution_chain_url)
50+
evolution_chain_data = evolution_chain_response.json()
51+
52+
evolution_chain = []
53+
current_evolution = evolution_chain_data['chain']
54+
55+
while current_evolution:
56+
evolution_chain.append(current_evolution['species']['name'].capitalize())
57+
if current_evolution['evolves_to']:
58+
current_evolution = current_evolution['evolves_to'][0]
59+
else:
60+
current_evolution = None
61+
62+
print(f"Evolutions chain: {', '.join(evolution_chain)}")
63+
64+
# Obtener juegos en los que aparece
65+
games = [game_info['version']['name'] for game_info in data['game_indices']]
66+
print(f"Games where it appears: {', '.join(games).capitalize()}")
67+
68+
69+
if __name__ == '__main__':
70+
print("Exercise")
71+
url = "https://retosdeprogramacion.com/roadmap/"
72+
print("GET to", url, request_url(url))
73+
url = "https://retosdeprogramacion.com/roadmap/1"
74+
print("GET to",url, request_url(url))
75+
76+
print("Extra")
77+
pokemon_name = input("Type pokemon's name or number: ")
78+
get_pokemon_data(pokemon_name)

0 commit comments

Comments
 (0)