1
00:00:05,280 --> 00:00:09,290
so it is nearly time for the challenge but
before we get into that what we want to do is go

2
00:00:09,290 --> 00:00:13,240
through and have a look at a common
source of errors when you're

3
00:00:13,240 --> 00:00:17,110
converting a dictionary that is
initialized using a literal into a shelve

4
00:00:17,110 --> 00:00:22,020
know this is one time unfortunately when
IntelliJ syntax checking doesn't help in

5
00:00:22,020 --> 00:00:25,230
detecting the error so it is worth being
aware of this so you know what to do in

6
00:00:25,230 --> 00:00:28,320
when this situation comes out so what we
gonna do is we're going to keep our

7
00:00:28,320 --> 00:00:32,369
recipes but we gonna make the dictionary a
little bit more complicated by adding to

8
00:00:32,369 --> 00:00:32,800
it

9
00:00:32,800 --> 00:00:38,080
a dictionary books so will use just two in this
case the recipes and the parts needed

10
00:00:38,080 --> 00:00:43,390
for performing maintenance on a motorcycle
now because this is a course on Python not motorcycle

11
00:00:43,390 --> 00:00:47,730
maintenance we are gonna keep it a bit
simple so a bit of the bike should move

12
00:00:47,730 --> 00:00:51,460
and if it doesn't will apply lubricant if it
does move and shouldn't will stick it

13
00:00:51,460 --> 00:00:55,260
down with gahfer tape so here is the dictionary we used to

14
00:00:55,260 --> 00:01:03,140
convert it into so I'll just type that up it is gonna be...

15
00:01:03,140 --> 00:01:14,280
....and its going to be....

16
00:01:33,830 --> 00:02:08,350
... and the last one for this book is pasta....

17
00:02:08,350 --> 00:02:19,170
and then we are going on to the
next book now and the next book is....

18
00:02:19,170 --> 00:02:30,440
we are going to call that maintenance.....

19
00:02:30,440 --> 00:02:41,490
and..so that is our definition as I said its a

20
00:02:41,490 --> 00:02:46,120
making a bit more complicated by adding
a dictionary books now so we got 2 book in

21
00:02:46,120 --> 00:02:50,720
there each with their own individual
parts in this case the first one was recipes

22
00:02:50,720 --> 00:02:55,150
the ingredients and the second one
was the things needed to fix a motor

23
00:02:55,150 --> 00:02:59,680
bike up or be it very simplistically and just to
confirm this works let's try doing a

24
00:02:59,680 --> 00:03:01,240
print....

25
00:03:01,240 --> 00:03:11,660
....print out the individual ingredients for soup...

26
00:03:11,660 --> 00:03:18,420
we should be able to do the same....

27
00:03:18,420 --> 00:03:43,840
...just to confirm that we can print out the ingredient lists....so lets just run that to confirm that it does work

28
00:03:43,840 --> 00:03:52,650
we got a typo there that should be recipes try again and you can see we got tin of soup which is the ingredient of soup we got eggs

29
00:03:52,650 --> 00:03:57,700
butter and milk which are ingredients for scrambled eggs and
gaffer tape which was correct for the

30
00:03:57,700 --> 00:04:02,670
loose key which is part of the
maintenance key pair so that's good so that is our key value

31
00:04:02,670 --> 00:04:08,840
so that is working fine now the conversion to shelve is
quite straightforward so after opening

32
00:04:08,840 --> 00:04:14,290
the shelve we just do a bit of editing to get it to
work so what we wanted to do is change the code

33
00:04:14,290 --> 00:04:19,160
and will do something like this, first is we import the shelve....

34
00:04:19,160 --> 00:04:32,490
and will put books..... and so
once we've done that all we can leave all

35
00:04:32,490 --> 00:04:35,060
the existing prints there because this
time it's now going to be printed to

36
00:04:35,060 --> 00:04:39,330
our shelve instead of printing out to the
screen so of course the last thing we need

37
00:04:39,330 --> 00:04:45,040
to do after all of that is do a books...and we just need to change it a little bit now

38
00:04:45,040 --> 00:04:48,700
because we've defined books above on line 4
what we need to do is we need to

39
00:04:48,700 --> 00:04:56,900
change this to put books.....and
down here what we need to do is we are getting

40
00:04:56,900 --> 00:05:06,700
down to this line and then we've got
books here....

41
00:05:06,700 --> 00:05:20,850
....and then of
course I have to fix this to get red of recipes and that should be fixed and of course right down at the end under books maintenance, of course there

42
00:05:20,850 --> 00:05:28,460
shouldn't be 2 braces it should be a single one
and Python being pedantic we need to to make sure that

43
00:05:28,460 --> 00:05:34,440
they line up correctly as well so want to remove any warnings
so once we've done that you can see now

44
00:05:34,440 --> 00:05:38,540
that we've got the green tick in the top
right hand corner of the screen so as

45
00:05:38,540 --> 00:05:45,520
far as IntelliJ is concerned everything is
good to go but when we try running it we get an error and you can

46
00:05:45,520 --> 00:05:49,210
see that the error that comes up is that
tuple indices must be integers

47
00:05:49,210 --> 00:05:56,560
or slices and not string or str now the
problem is caused over here on line 8 by

48
00:05:56,560 --> 00:05:57,210
leaving a

49
00:05:57,210 --> 00:06:00,920
comma at the end of that line now this
is a very easy mistake to make when

50
00:06:00,920 --> 00:06:04,760
converting a literal dictionary into
individual shelve assignments and what

51
00:06:04,760 --> 00:06:08,110
we've actually ended up is a
value to the key recipes that is a

52
00:06:08,110 --> 00:06:08,710
tuple

53
00:06:08,710 --> 00:06:12,690
consisting of a dictionary and an empty
entry and we can see that by changing

54
00:06:12,690 --> 00:06:23,520
line 12 if we just temporarily comment out these other ones out and we just
print out recipes instead of the ingredients

55
00:06:23,520 --> 00:06:29,520
for soup like that after removing the comma it
shouldn't have been there if we run it again

56
00:06:29,520 --> 00:06:40,080
now we can sort of see what the result is
we'll just scroll over you can see that the problem is fixed so

57
00:06:40,080 --> 00:06:46,720
it's really quite important when your converting a dictionary
into a shelve so keep that in mind we really

58
00:06:46,720 --> 00:06:50,060
need to take care to ensure that there's
no additional commas or curly brackets

59
00:06:50,060 --> 00:06:55,050
where you will get odd results and error that are
sometimes can be hard to debug so do

60
00:06:55,050 --> 00:06:58,400
keep that in mind when using shelve to
convert it as it was shown in this

61
00:06:58,400 --> 00:07:03,280
example ok so on with the challenge
so what I'm going to do now is create a new

62
00:07:03,280 --> 00:07:13,290
file so I'm gonna call this challenge and although we did say a shelve
perhaps not the best choice for

63
00:07:13,290 --> 00:07:17,720
persisting data in an adventure game we
have set up fairly complex dictionaries in

64
00:07:17,720 --> 00:07:18,350
this game

65
00:07:18,350 --> 00:07:21,870
and as is its structure that you'll
understand because we used it we're

66
00:07:21,870 --> 00:07:24,980
going to use that so you can focus on
the code changes rather than trying to

67
00:07:24,980 --> 00:07:28,270
understand yet another data structure so
this challenge will be good practice for

68
00:07:28,270 --> 00:07:33,080
switching dictionary to a shelve and also
vice versa should you need to but rest

69
00:07:33,080 --> 00:07:37,420
assured with that said that will not be using
shelves to persists the basic game data as

70
00:07:37,420 --> 00:07:40,150
we complete the game I'm gonna paste in
and let you know

71
00:07:40,150 --> 00:07:47,680
to show you what the challenges is ok so
here is the challenge and I've paste some code in

72
00:07:47,680 --> 00:07:50,830
from a previous challenge so what you
need to do is modify the program from

73
00:07:50,830 --> 00:07:54,620
the second dictionary challenge of
of lecture 56 and that's what these code

74
00:07:54,620 --> 00:07:59,250
is from to use shelves instead of the dictionaries
that we of course used in that challenge

75
00:07:59,250 --> 00:08:03,970
and you want to do this by creating two
programs first one should be called cave

76
00:08:03,970 --> 00:08:08,370
underscore initialize dot py and that
should create the two shelves which are

77
00:08:08,370 --> 00:08:12,910
locations and vocabulary with the
appropriate keys and values and second

78
00:08:12,910 --> 00:08:16,840
file that you want to create is called cave underscore
game dot py and that

79
00:08:16,840 --> 00:08:21,250
will then used the two shelves instead of
using dictionaries so apart from opening

80
00:08:21,250 --> 00:08:22,840
and closing the shelves

81
00:08:22,840 --> 00:08:27,790
cave_game would need only to change to
the actual code remember that shelve keys

82
00:08:27,790 --> 00:08:32,580
must be strings so that's very important to
keep in mind and just to be clear the

83
00:08:32,580 --> 00:08:36,400
cave_game py will contain the code
from line 45 on words below so in other

84
00:08:36,400 --> 00:08:40,679
words the code starting with this loc = 1 through to the end everything before

85
00:08:40,679 --> 00:08:47,060
that as you can see here on lines 14 through 43
are inclusive that ultimately gonna be modified

86
00:08:47,060 --> 00:08:51,100
to use shelve being cave_initialize.py so

87
00:08:51,100 --> 00:08:55,330
essentially the process will be that you will run
cave_initialized.py and that will

88
00:08:55,330 --> 00:09:00,030
create the necessary shelves then of
course in the cave_game.py that

89
00:09:00,030 --> 00:09:04,190
is gonna be using those shelves and
that where you actually run the

90
00:09:04,190 --> 00:09:08,330
game itself so that's the challenge now is
the time to pause the video and go away

91
00:09:08,330 --> 00:09:12,310
and see if you can figure out that challenge
and when you done come back and you can

92
00:09:12,310 --> 00:09:20,270
compare what you come up with our solution
so we'll see when you get back

93
00:09:20,270 --> 00:09:28,400
ok so how did you get on, hopefully you got it to work so what we going to do is start
on cave_initialize.py so I want to take

94
00:09:28,400 --> 00:09:36,900
a copy of that and I'm going to create
the new Python file and call that cave_initialized.py

95
00:09:36,900 --> 00:09:42,200
and that's going to be our initialization
code obviously we got the code here that

96
00:09:42,200 --> 00:09:45,560
we're going to be reliant on and that does need
to be changed so we're going to do is

97
00:09:45,560 --> 00:09:48,680
I'm going to just take that code so you
can see what I'm doing to change it

98
00:09:48,680 --> 00:09:54,710
the code up to an including line 43 I'm gonna paste that in

99
00:09:54,710 --> 00:10:01,590
two our cave_initialize.py then I'm going to go back up
to line 1 and start making some

100
00:10:01,590 --> 00:10:09,840
changes so first I'm gonna import shelve....obviously need that
so you can write shelves out and will start with location so

101
00:10:09,840 --> 00:10:21,560
locations....that is ultimately going to be the name of our shelve so what we wanna do now is convert these remembering that we

102
00:10:21,560 --> 00:10:27,670
need to use strings as keys for a shelve and
obviously as you can see right now this

103
00:10:27,670 --> 00:10:32,440
is not the case we're not
using strings there we are using entered

104
00:10:32,440 --> 00:10:36,340
integers so a way to changes could
be to changes this code a little bit so we could

105
00:10:36,340 --> 00:10:44,560
put locations...put the location in as a string as you can see there and

106
00:10:44,560 --> 00:10:46,330
set that to equals

107
00:10:46,330 --> 00:10:52,370
so get rid of that first part, and the exits is good and the name exits
is fine but we will then get rid of the

108
00:10:52,370 --> 00:11:02,290
the comma in the end of the line we are gonna do that for each line
so I'm gonna copy some of this code so just to be a bit quicker go

109
00:11:02,290 --> 00:11:17,430
back to the start of the line paste that in, this is location 1...and the next one

110
00:11:17,430 --> 00:11:24,050
location 2...and I'm deleting the commas as you can see each time, remember we talked about

111
00:11:24,050 --> 00:11:44,080
that earlier in the video how that's
important location 3....and 4....and the last one obviously

112
00:11:44,080 --> 00:11:55,730
5... and we get rid of that brace which we no longer
need and we can probably fix these up just to

113
00:11:55,730 --> 00:12:08,450
indent them so that Python is happy with
the indentation and I'm getting a warning there ok so that fixed all those up so

114
00:12:08,450 --> 00:12:12,380
that is the first part and we need to do the
similar thing now for second part

115
00:12:12,380 --> 00:12:16,960
obviously the second part vocabulary
is using a key currently a string and not

116
00:12:16,960 --> 00:12:20,580
as an integer but we still need to
convert it into that format so doing

117
00:12:20,580 --> 00:12:33,210
something like...and that's the code I'm going to copy actually I won't do that because it is a typo

118
00:12:33,210 --> 00:12:39,830
vocabulary...will copy that part as we did before and I'm going to

119
00:12:39,830 --> 00:13:04,900
come over here and copy that part quit....and of course what I need to do their is start making the shelves so...and now we can continue on with our

120
00:13:04,900 --> 00:13:14,190
copy and paste....

121
00:13:14,190 --> 00:14:02,290
..and next one is east...and next one is west....and road...hill...

122
00:14:02,290 --> 00:14:49,300
..valley...and last one is forest....and last thing to do here in order is we put vocabulary.close and the location.close and probably we could do there is we could close on this line because there is no reason to have it close rather than

123
00:14:49,300 --> 00:14:51,490
having to shelves opening at once not that it really matters

124
00:14:51,490 --> 00:14:56,510
but it is good practice to have all the
code in some areas so we are opening the shelve here

125
00:14:56,510 --> 00:15:00,890
we're performing the operation when want then we
close it immediately and likewise with vocabulary

126
00:15:00,890 --> 00:15:03,570
add some spaces so its a bit more readable

127
00:15:03,570 --> 00:15:09,540
that's should be the it that is the initialization set
up and we will add a new line there to keep you it happy

128
00:15:09,540 --> 00:15:13,300
and we should have a green tick in the top
right hand corner which we've got so that's good

129
00:15:13,300 --> 00:15:17,830
so just a recap what we had to do was because
shelve keys have to be strings which we

130
00:15:17,830 --> 00:15:21,930
talked about we had to change all the numeric
entries representing the locations

131
00:15:21,930 --> 00:15:26,930
firstly two strings and you can see they are all done now on lines 5 to 25 and then

132
00:15:26,930 --> 00:15:31,390
we did a similar thing for the vocabulary
even though they were already strings

133
00:15:31,390 --> 00:15:35,570
and not integers we still just change the
format to make it a bit easier and a bit

134
00:15:35,570 --> 00:15:38,870
more consistent so at this point we're
actually done so we've got the

135
00:15:38,870 --> 00:15:42,440
initialization down cave_initialized.py in the next video

136
00:15:42,440 --> 00:15:46,490
will come back and we'll make the
changes to cav_game so that

137
00:15:46,490 --> 00:15:48,300
we can get that working so see you in the
next video

