Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10 - Python y #11 - Python #6547

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Retos/Reto #10 - LA API [Media]/python/restevean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Exercise
"""
import requests


def get_pokemon_data(pokemon_identifier):
base_url = "https://pokeapi.co/api/v2/pokemon/"
url = f"{base_url}{pokemon_identifier.lower()}"

response = requests.get(url)

if response.status_code != 200:
print(
f"Error: No se pudo obtener información para {pokemon_identifier}. Código de estado: {response.status_code}")
return

data = response.json()
print(f"Name: {data['name'].capitalize()}")
print(f"ID: {data['id']}")
print(f"Weight: {data['weight']/10} kg")
print(f"Height: {data['height']*10} cm")

types = [type_info['type']['name'] for type_info in data['types']]
print(f"Tipe(s): {', '.join(types).capitalize()}")

# Obtener cadena de evoluciones
species_url = data['species']['url']
species_response = requests.get(species_url)
species_data = species_response.json()
evolution_chain_url = species_data['evolution_chain']['url']

evolution_chain_response = requests.get(evolution_chain_url)
evolution_chain_data = evolution_chain_response.json()

evolution_chain = []
current_evolution = evolution_chain_data['chain']

while current_evolution:
evolution_chain.append(current_evolution['species']['name'].capitalize())
if current_evolution['evolves_to']:
current_evolution = current_evolution['evolves_to'][0]
else:
current_evolution = None

print(f"Evolutions chain: {', '.join(evolution_chain)}")

# Obtener juegos en los que aparece
games = [game_info['version']['name'] for game_info in data['game_indices']]
print(f"Games where it appears: {', '.join(games).capitalize()}")


if __name__ == '__main__':
pokemon_name = input("Type pokemon's name or number: ")
get_pokemon_data(pokemon_name)
35 changes: 35 additions & 0 deletions Retos/Reto #11 - URL PARAMS [Fácil]/python/restevean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Exercise
"""


def get_url_parameters(url):
# Split the URL into the base part and the query string
parts = url.split('?')

# If there are no parameters, return an empty list
if len(parts) < 2:
return []

# Get the query string part
query_string = parts[1]

# Split the query string into key-value pairs
pairs = query_string.split('&')

# Extract the values from the key-value pairs
values = []
for pair in pairs:
key_value = pair.split('=')
if len(key_value) == 2:
values.append(key_value[1])
else:
values.append('')

return values


if __name__ == '__main__':
url = "https://retosdeprogramacion.com?year=2023&challenge=0"
parameters = get_url_parameters(url)
print(parameters)