1
00:00:05,380 --> 00:00:09,510
in the previous video we did our
first part of the challenge and that's

2
00:00:09,510 --> 00:00:15,510
where we set up this cave underscore initialize dot py
which ultimately save

3
00:00:15,510 --> 00:00:20,140
our locations and vocabulary
dictionaries to convert them to shelves

4
00:00:20,140 --> 00:00:25,230
what we wanna do now is we want to change
the code that runs the game the actual

5
00:00:25,230 --> 00:00:29,490
game itself and make the changes so that
instead of using a dictionary it uses

6
00:00:29,490 --> 00:00:33,900
the shelves created we created in this so what I'm going
to do is go back to

7
00:00:33,900 --> 00:00:39,380
challenge you see that cave_game.py that is the file name we want to use for the

8
00:00:39,380 --> 00:00:47,430
game part of it so we are going to
create that new python and its going to be

9
00:00:47,430 --> 00:00:53,580
cave_game.py and what I'm gonna
start with is just a copy this code in line 45

10
00:00:53,580 --> 00:01:00,060
from the challenge.py through to the
end and paste that to

11
00:01:00,060 --> 00:01:04,650
cave_game.py so that's the basic
game so obviously we got some erros showing

12
00:01:04,650 --> 00:01:09,000
at the moment because we haven't got the
locations and the vocabulary set up so we

13
00:01:09,000 --> 00:01:13,740
need to actually do that we need to come
up here initially on line 1 we need to

14
00:01:13,740 --> 00:01:21,210
import shelve of course because we are going to use the shelve module.....

15
00:01:35,010 --> 00:01:45,630
and the other thing we need
to do while we remember is down here we ill close

16
00:01:45,630 --> 00:01:52,480
them so....always have to do that

17
00:01:52,480 --> 00:01:59,020
remember, if we are not using with to close things manually which is we have done there

18
00:01:59,020 --> 00:02:03,760
the other change we need to make is on line 6
you notice it currently we got loc equals

19
00:02:03,760 --> 00:02:07,990
one and that's an integer I want to
change that so its initialize to a

20
00:02:07,990 --> 00:02:12,810
string because of course if you recall
in cave_initilaize.py we

21
00:02:12,810 --> 00:02:17,600
set it up so we will use strings because
of course shelves need to use strings

22
00:02:17,600 --> 00:02:21,540
as keys s so because locations is gonna
be key ultimately that will need to be

23
00:02:21,540 --> 00:02:26,850
a string so I'm gonna put single quotes around that
so that is now a string and the other thing we need to do then

24
00:02:26,850 --> 00:02:30,510
is on line 12 we're testing to see
whether 0 has been received

25
00:02:30,510 --> 00:02:35,480
because it's a string we need to do a test
against a string string comparison and

26
00:02:35,480 --> 00:02:42,680
that's going to be two single quotes to do that comparison now

27
00:02:42,680 --> 00:02:45,600
that should be it so what we'll do is
we'll give this a run just to make sure

28
00:02:45,600 --> 00:02:53,640
that does work and I'm going to run this
the first time so that, and just check

29
00:02:53,640 --> 00:03:03,710
the I called that he correct name......vocabulary that should be correct we will just

30
00:03:03,710 --> 00:03:10,480
run it again you can see the problem here was in a line 8
so lets checked that out

31
00:03:10,480 --> 00:03:15,880
and we acutally see that so lets close this down so it is having a
problem there with line 8 at which is

32
00:03:15,880 --> 00:03:19,620
available exits and the
problem is if we run it again to check, it is

33
00:03:19,620 --> 00:03:25,120
having a problem with the keys
themselves so let's go back to cave_initialize

34
00:03:25,120 --> 00:03:29,980
just to make sure that I
haven't messed up something and that seems

35
00:03:29,980 --> 00:03:35,050
that I've made that error again the one I made about
three or four times or more in this

36
00:03:35,050 --> 00:03:40,180
course is of course when I ran the
initialize hadn't been set up the first

37
00:03:40,180 --> 00:03:43,460
time and I can see that it hasn't been set
up so consequently the error we are getting

38
00:03:43,460 --> 00:03:49,390
here when we run these cave_game I'm running
now is basically saying well those files

39
00:03:49,390 --> 00:03:52,890
don't exists and the reason they don't
exist is of course I didn't run it so

40
00:03:52,890 --> 00:03:57,140
what happened was the challenge code
was actually run or actually more likely the

41
00:03:57,140 --> 00:04:00,570
example.py was run and I wasn't
really paying attention to the screen

42
00:04:00,570 --> 00:04:04,820
which I should have so what we need to
do now is go back to cave_initialize.py

43
00:04:04,820 --> 00:04:10,860
that's correct as it is right click
that run cave_initialize that's going to

44
00:04:10,860 --> 00:04:16,040
create a our shelves witch I should have
done and then go back to cave_game than

45
00:04:16,040 --> 00:04:24,430
any other code changes we should be able to run that as well and now we are not getting an error what a surprised Tim if you acutally rung the right file it might work

46
00:04:24,430 --> 00:04:28,240
and we will do a bit a testing to make sure that
it's working

47
00:04:28,240 --> 00:04:34,690
so go north and we're getting an error
there and ok int object has no attribute

48
00:04:34,690 --> 00:04:38,650
I think what I've done is I've forgotten to
change something in the cave_initialize

49
00:04:38,650 --> 00:04:43,080
so I'll close this down and go
back and have a look and actually what I

50
00:04:43,080 --> 00:04:47,310
haven't done which I should have done
you notice we are using a key for the locations it

51
00:04:47,310 --> 00:04:53,040
was an integer and I change this correctly to a string but the location

52
00:04:53,040 --> 00:04:58,740
is also used in the exits as you can see
in the named exits and I haven't changed

53
00:04:58,740 --> 00:05:02,570
the value to a string now the values has to be
string because we gonna be comparing the

54
00:05:02,570 --> 00:05:07,150
string value here to the location we entered there so we could convert

55
00:05:07,150 --> 00:05:10,700
that to an integer but  probably the
best practices because we are gonna be

56
00:05:10,700 --> 00:05:14,340
continually converting it to get the
data right the first time so in that

57
00:05:14,340 --> 00:05:18,170
situation I recommend you do is you
changed the data type your going to be using

58
00:05:18,170 --> 00:05:21,100
which in this case is gonna be
string so I'm gonna change all of these

59
00:05:21,100 --> 00:05:31,040
to strings...and that should fix the problem once we are done with it

60
00:05:31,040 --> 00:05:43,990
you can see that IntelliJ tries to be helpful

61
00:05:43,990 --> 00:05:49,260
I've put a single quote around before a number that's fine but if I put it after it adds two because it thinks you

62
00:05:49,260 --> 00:05:52,620
wanna put some numbers something in the
middle of it so it all a matter of cursor

63
00:05:52,620 --> 00:06:01,340
position to get this right

64
00:06:01,340 --> 00:06:22,950
....and we should find that it will work after we do this, couple more to do, and that should be that so what we've done is we've
converted

65
00:06:22,950 --> 00:06:28,620
hopefully all of them hopefully I didn't miss some strings and actually there is a couple I miss

66
00:06:28,620 --> 00:06:41,730
there Tim what are you doing? so now I think we are done, no there is still another one, goodness I'm having a bad day

67
00:06:41,730 --> 00:06:46,730
Ok I cannot see any more now so what we're
going to do is rerun this we should recreate

68
00:06:46,730 --> 00:06:51,460
the shelve and then when we go back to cave and its going to work
so again just to be clear the reason it

69
00:06:51,460 --> 00:06:52,490
wasn't working

70
00:06:52,490 --> 00:06:57,770
was we converted the locations two
strings here and then in here we had a

71
00:06:57,770 --> 00:07:03,150
location and direction to go to but we
also had the location stores integer so

72
00:07:03,150 --> 00:07:06,560
what was happening was there was a
string integer comparison taking place

73
00:07:06,560 --> 00:07:12,860
in here so it successfully printed
it out but when we try to do a comparison but we ultimately

74
00:07:12,860 --> 00:07:18,050
crash because it was that string and
int comparison that is occurring in game so

75
00:07:18,050 --> 00:07:22,140
lets go back to first to cave_initialize right click that and run that to

76
00:07:22,140 --> 00:07:31,050
recreate the shelve that's been done so if we go back to cave_game
right click and run that a see if its going to

77
00:07:31,050 --> 00:07:43,370
work for us so type north south east west will
try typing north that's good and try typing quit

78
00:07:43,370 --> 00:07:44,230
and that is it so

79
00:07:44,230 --> 00:07:49,810
albeit with a bit of bother we got to actually the
end and I managed to get this working so

80
00:07:49,810 --> 00:07:54,780
that's good so that was our solution the solution
myself and JP came up with so hope you

81
00:07:54,780 --> 00:07:58,640
enjoy that and what was quite
interesting I felt was that really there is

82
00:07:58,640 --> 00:08:02,900
very few changes required to the actual
game so we swipe out the dictionary

83
00:08:02,900 --> 00:08:04,400
that was in the game

84
00:08:04,400 --> 00:08:10,980
quite easily by replacing the lines 3 and 4, 34 and 35 and we had to little tests

85
00:08:10,980 --> 00:08:14,720
we need to change we need to
change the test on line 12

86
00:08:14,720 --> 00:08:19,730
and also change the variable type from
an integer to a string on line 6 and of

87
00:08:19,730 --> 00:08:24,140
course the bulk of the changes
were in the data in this format where we

88
00:08:24,140 --> 00:08:28,610
actually wrote the correct
dictionaries to shelves so this is

89
00:08:28,610 --> 00:08:32,510
what we always recommend you do is get
you data correct so don't write the

90
00:08:32,510 --> 00:08:37,870
program to sort of manipulate problems
in the data so in other words what

91
00:08:37,870 --> 00:08:41,000
I could have done there was I mention
when I was converting these which

92
00:08:41,000 --> 00:08:44,440
should have been two strings by putting
the single quotes around them we could

93
00:08:44,440 --> 00:08:47,500
come back to the code here and
started doing a comparison and checking

94
00:08:47,500 --> 00:08:51,930
converting integers back to strings and
so forth and doing comparison like that

95
00:08:51,930 --> 00:08:56,680
but that would all be sort of coding to
cover up the fact that time we made a

96
00:08:56,680 --> 00:09:00,540
mistake with our data by getting the
data correct it means we don't have

97
00:09:00,540 --> 00:09:05,710
to do any extra coding in the game and that
applies to a lot of coding in general

98
00:09:05,710 --> 00:09:10,370
getting the data correct is a great first step
to being able to with a minimal amount of

99
00:09:10,370 --> 00:09:16,680
code go through and manipulate use that data as we did so as it
turns out there's very few lines of code

100
00:09:16,680 --> 00:09:21,060
that were needed to get the
game working with shelves so that's it

101
00:09:21,060 --> 00:09:25,590
that's the shelve challenge I hope you've
enjoyed that so that's actually also the

102
00:09:25,590 --> 00:09:29,890
end of this section so in the next
section we gonna be starting to talk

103
00:09:29,890 --> 00:09:34,140
about modules and functions so we look
forward to taking you through that and

104
00:09:34,140 --> 00:09:35,470
we'll see you in the next section

