-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_a_monster.py
More file actions
59 lines (52 loc) · 1.79 KB
/
move_a_monster.py
File metadata and controls
59 lines (52 loc) · 1.79 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
53
54
55
56
57
58
59
import random
# This is the map of our house.
# Notice the value for 'item' is now a list.
# We need this so we can have multiple items in a room.
rooms = {
'Hall': {
'south': 'Kitchen',
'east': 'Dining Room',
'item': ['key']
},
'Kitchen': {
'north': 'Hall',
'item': ['monster']
},
'Dining Room': {
'west': 'Hall',
'item': ['potion']
}
}
def move_monster():
"""This function finds out where the monster is
and moves it to a random room of the house."""
current_room = None
monster_room = None
# Find the current room of the monster
# We can iterate over the room key and its value at the same time!
# room is the outermost key:value pair
# details is the inner dictionaires (south, item, north, etc.)
# Mind explosion!
# rooms.items() allows you to loop across all the key value pairs
# in the entire dictionary.
for room, details in rooms.items():
if 'monster' in details.get('item', []):
current_room = room
monster_room = room
break
# Randomly select a new room for the monster
# The .keys dictionary method returns a list-like object
# We have to force it to be a real list withe the list() function
new_room = random.choice(list(rooms.keys()))
# Move the monster to the new room
# Use the list methods .remove() and .append() to remove and add
# the monster to different lists in your dictionary
rooms[current_room]['item'].remove('monster')
rooms[new_room]['item'].append('monster')
print('Monster moved from', current_room, 'to', new_room)
# Move the monster
print("Here's the old dictionary.")
print(rooms)
move_monster()
print("Here's the new dictionary after the monster moves. Scary!")
print(rooms)