-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokedex.py
More file actions
52 lines (43 loc) · 1.29 KB
/
pokedex.py
File metadata and controls
52 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cmd
import requests
class Pokemon(object):
def __init__(self, data):
self.name = data['name']
self.id = data['id']
self.types = [t['type']['name'] for t in data['types']]
def __str__(self):
return 'Pokemon #{}: {}'.format(self.id, self.name)
class Pokedex(cmd.Cmd):
intro = 'Welcome to the PokeDex. Type help for a list of commands.\n'
prompt = '\n> '
collection = {}
def do_catch(self, arg):
'Add a pokemon to the collection'
if arg in self.collection:
print('{} is already in your collection'.format(arg))
else:
r = requests.get('http://pokeapi.co/api/v2/pokemon/{}'.format(arg))
data = r.json()
new = Pokemon(data)
self.collection[arg] = new
print('Caught pokemon {}'.format(arg))
def do_lookup(self, arg):
'Find a pokemon from the collection by name'
# Look in the collection
if not arg in self.collection:
print("You haven't caught {} yet".format(arg))
else:
print(self.collection[arg])
def do_print_collection(self, arg):
'Print a list of the pokemon in your collection'
for key in self.collection.keys():
print(key)
# def do_list(self, arg):
# 'Print a list of the given resource'
# # Get a list from the API
# pass
def do_quit(self, arg):
'Exit the PokeDex'
return True
if __name__ == "__main__":
Pokedex().cmdloop()