File tree 1 file changed +60
-0
lines changed
Roadmap/20 - PETICIONES HTTP/python
1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ import requests as re
2
+
3
+
4
+ def check_connection ():
5
+ try :
6
+ r = re .get ("https://retosdeprogramacion.com" )
7
+ print ("La petición fue existosa" )
8
+ except Exception as e :
9
+ print (f"Error: { e } " )
10
+
11
+
12
+ def get_pokemon (identifier : str ):
13
+ try :
14
+ r = re .get (f"https://pokeapi.co/api/v2/pokemon/{ identifier } " )
15
+ r_json = r .json ()
16
+
17
+ types = r_json ['types' ]
18
+
19
+ print (f"\n ID: { r_json ['id' ]} " )
20
+ print (f"Nombre: { r_json ['name' ]} " )
21
+ print (f"Peso: { r_json ['weight' ]} libras" )
22
+ print (f"Altura: { r_json ['height' ]} pies" )
23
+ print (f"Tipos:" )
24
+ for type in types :
25
+ print (f" - { get_type (type ['type' ]['url' ])} " )
26
+
27
+ except Exception as e :
28
+ print ("El Pokemon o ID no son válidos" )
29
+
30
+
31
+ def get_type (url : str ):
32
+ r = re .get (url )
33
+ r_json = r .json ()
34
+ return r_json ['name' ]
35
+
36
+
37
+ def main ():
38
+ finish = False
39
+
40
+ while not finish :
41
+
42
+ print ("\n Selecciona una opción:\n "
43
+ "1 - Introducir nombre o ID del Pokemon\n "
44
+ "2 - Salir" )
45
+ option = int (input ("> " ))
46
+
47
+ match option :
48
+ case 1 :
49
+ print ("\n Escribe el nombre o identificador del Pokemon" )
50
+ identifier = input ("> " )
51
+ get_pokemon (identifier )
52
+ case 2 :
53
+ finish = True
54
+ print ("Hasta pronto 👋" )
55
+ case _:
56
+ print ("⚠️ Opción seleccionada no válida" )
57
+
58
+
59
+ if __name__ == '__main__' :
60
+ main ()
You can’t perform that action at this time.
0 commit comments