-
Notifications
You must be signed in to change notification settings - Fork 3
/
JZ_C2Exercise.py
292 lines (281 loc) · 8.76 KB
/
JZ_C2Exercise.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
fruit = {"orange": "a sweet, orange, citrus fruit",
"apple": "good for making cider",
"lemon": "a sour, yellow citrus fruit",
"grape": "a small, sweet fruit growing in bunches",
"lime": "a sour, green citrus fruit",
"apple": "round and crunchy"}
print(fruit)
print(fruit["lemon"])
for snack in fruit:
print(fruit[snack])
#
here lemon is a key as a dictionary can be accesed using a key
#
bike = {"make": "Honda", "model": "250 dream", "colour": "red", "engine_size": 250}
print(bike["engine_size"])
print(bike["colour"])
#
a key in the dictionary is unique
ADDING A NEW ENTRY TO A DICTIONARY
fruit["pear"] = "an odd shaped apple"
print(fruit)
fruit["lime"] = "great with tequila"
print(fruit)
#
REMOVE a dictionary
USE OF DEL COMMAND
#
del fruit["lemon"]
#
REMOVING CONTENT FROM A DICTIONARY
#
fruit.clear()
print(fruit)
#
RETRIEVING ITEM FROM A DICTIONARY THAT DOESN'T EXISTS
#
print(fruit["tomato"])
#
USE OF GET COMMAND TO ACCESS THE DESCRIPTION OF A KEY FROM A DICTIONARY
#
print(fruit)
while True:
dict_key = input("Please enter a fruit: ")
if dict_key == "quit":
break
if dict_key in fruit:
description = fruit.get(dict_key)
print(description)
else:
print("we don't have a " + dict_key)
print(fruit)
while True:
dict_key = input("Please enter a fruit: ")
if dict_key == "quit":
break
description = fruit.get(dict_key, "We don't have a " + dict_key)
print(description)
fruit = {"orange": "a sweet, orange, citrus fruit",
"apple": "good for making cider",
"lemon": "a sour, yellow citrus fruit",
"grape": "a small, sweet fruit growing in bunches",
"lime": "a sour, green citrus fruit"}
print(fruit)
for i in range(10):
for snack in fruit:
print(snack + " is " + fruit[snack])
print("="*40)
#
ORDERING A DICTIONARY
#
ordered_keys = list(fruit.keys())
ordered_keys.sort()
ordered_keys = sorted(list(fruit.keys()))
for f in ordered_keys:
print(f + " - " + fruit[f])
ordering a dictionary without using a list
for f in sorted(fruit.keys()):
for f in fruit:
print(f + " - " + fruit[f])
#
USE OF VALUE COMMAND
#
for val in fruit.values():
print(val)
print('-' * 40)
for key in fruit:
print(fruit[key])
#
fruit_keys = fruit.keys()
print(fruit_keys)
print(fruit.keys())
print(fruit.values())
fruit["tomato"] = "not nice with ice cream"
print(fruit_keys)
#
USE OF ITEM COMMAND
#
print(fruit)
print(fruit.items())
f_tuple = tuple(fruit.items())
print(f_tuple)
#
for snack in f_tuple:
item, description = snack
print(item + " is " + description)
#
making dictionary from a tuple
print(dict(f_tuple))
#
UPDATE TWO DICTIONARIES OR JOINING TWO DICTIONARIES
#
# fruit = {"orange": "a sweet, orange, citrus fruit",
# "apple": "good for making cider",
# "lemon": "a sour, yellow citrus fruit",
# "grape": "a small, sweet fruit growing in bunches",
# "lime": "a sour, green citrus fruit"}
# print(fruit)
#
# veg = {"cabbage": "every child's favourite",
# "sprouts": "mmmm, lovely",
# "spinach": "can i have some more fruit ,please"}
#
# print(veg)
#
# veg.update(fruit)
# print(veg)
#
# fruit.update(veg)
# print(fruit)
it will return none vale
#
# print(fruit.update(veg))
# print(fruit)
#
USE OF COPY COMMAND IN DICTIONARY
#
# nice_and_nasty = fruit.copy()
# nice_and_nasty.update(veg)
# print(nice_and_nasty)
#
GAME
#
Modify the program so that the exits is a dictionary rather than a list,
with the keys being the numbers of the locations and the values being
dictionaries holding the exits (as they do at present). No change should
be needed to the actual code.
Once that is working, create another dictionary that contains words that
players may use. These words will be the keys, and their values will be
a single letter that the program can use to determine which way to go.
#
#
# locations = {0: "You are sitting in front of a computer learning Python",
# 1: "You are standing at the end of a road before a small brick building",
# 2: "You are at the top of a hill",
# 3: "You are inside a building, a well house for a small stream",
# 4: "You are in a valley beside a stream",
# 5: "You are in the forest"}
#
# exits = {0: {"Q": 0},
# 1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
# 2: {"N": 5, "Q": 0},
# 3: {"W": 1, "Q": 0},
# 4: {"N": 1, "W": 2, "Q": 0},
# 5: {"W": 2, "S": 1, "Q": 0} }
#
# namedExits = {1: {"2": 2, "3": 3, "5": 5, "4": 4},
# 2: {"5": 5},
# 3: {"1": 1},
# 4: {"1": 1, "2": 2},
# 5: {"2": 2, "1": 1}}
#
# vocabulary = { "QUIT": "Q",
# "NORTH": "N",
# "SOUTH": "S",
# "EAST": "E",
# "WEST": "W",
# "ROAD": "1",
# "HILL": "2",
# "BUILDING": "3",
# "VALLEY": "4",
# "FOREST": "5" }
#
print(locations[0].split())
print(locations[3].split(","))
print(' '.join(locations[0].split()))
#
# loc = 1
# while True:
# availableExits = ", ".join(exits[loc].keys())
#
# print(locations[loc])
#
# if loc == 0:
# break
# else:
# allExits = exits[loc].copy()
# allExits.update(namedExits[loc])
#
# direction = input("Available exits are " + availableExits + " ").upper()
# print()
Parse the user input, using our vocabulary dictionary if necessary
# if len(direction) > 1: # more than one letter, so check vocab
# words = direction.split()
# for word in words:
# if word in vocabulary:
# direction = vocabulary[word]
# break
#
# if direction in allExits:
# loc = allExits[direction]
# else:
# print("You cannot go in that direction")
#
Challenge time!
#
We have mentioned that the data for the Adventure game could be organised in many
different ways. We've created another way for you.
Your mission, if you choose to accept it, is to
change the code to make it work.
Below is the the complete program from the last video, but with the
locations dictionary modified so that everything is in a single dictionary.
N.B. Yes the code has some errors, thats what you need to fix!
#
# locations = {0: {"desc": "You are sitting in front of a computer learning Python",
# "exits": {},
# "namedExits": {}},
# 1: {"desc": "You are standing at the end of a road before a small brick building",
# "exits": {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0},
# "namedExits": {"2": 2, "3": 3, "5": 5, "4": 4}},
# 2: {"desc": "You are at the top of a hill",
# "exits": {"N": 5, "Q": 0},
# "namedExits": {"5": 5}},
# 3: {"desc": "You are inside a building, a well house for a small stream",
# "exits": {"W": 1, "Q": 0},
# "namedExits": {"1": 1}},
# 4: {"desc": "You are in a valley beside a stream",
# "exits": {"N": 1, "W": 2, "Q": 0},
# "namedExits": {"1": 1, "2": 2}},
# 5: {"desc": "You are in the forest",
# "exits": {"W": 2, "S": 1, "Q": 0},
# "namedExits": {"2": 2, "1": 1}}
# }
#
# vocabulary = {"QUIT": "Q",
# "NORTH": "N",
# "SOUTH": "S",
# "EAST": "E",
# "WEST": "W",
# "ROAD": "1",
# "HILL": "2",
# "BUILDING": "3",
# "VALLEY": "4",
# "FOREST": "5"}
#
# loc = 1
# while True:
# availableExits = ", ".join(locations[loc]["exits"].keys())
#
# print(locations[loc]["desc"])
#
# if loc == 0:
# break
# else:
# allExits = locations[loc]["exits"].copy()
# allExits.update(locations[loc]["namedExits"])
#
# direction = input("Available exits are " + availableExits).upper()
# print()
#
Parse the user input, using our vocabulary dictionary if necessary
# if len(direction) > 1: # more than 1 letter, so check vocab
# words = direction.split()
# for word in words:
# if word in vocabulary: # does it contain a word we know?
# direction = vocabulary[word]
# break
#
# if direction in allExits:
# loc = allExits[direction]
# else:
# print("You cannot go in that direction")