WEBVTT 1 00:00:01.960 --> 00:00:06.090 in the previous video we did our first part of the challenge and that's 2 00:00:06.090 --> 00:00:12.099 where we set up this cave underscore initialize dot py which ultimately save 3 00:00:12.099 --> 00:00:16.720 our locations and vocabulary dictionaries to convert them to shelves 4 00:00:16.720 --> 00:00:21.810 what we wanna do now is we want to change the code that runs the game the actual 5 00:00:21.810 --> 00:00:26.079 game itself and make the changes so that instead of using a dictionary it uses 6 00:00:26.079 --> 00:00:30.480 the shelves created we created in this so what I'm going to do is go back to 7 00:00:30.480 --> 00:00:35.960 challenge you see that cave_game.py that is the file name we want to use for the 8 00:00:35.960 --> 00:00:44.019 game part of it so we are going to create that new python and its going to be 9 00:00:44.019 --> 00:00:50.160 cave_game.py and what I'm gonna start with is just a copy this code in line 45 10 00:00:50.160 --> 00:00:56.649 from the challenge.py through to the end and paste that to 11 00:00:56.649 --> 00:01:01.230 cave_game.py so that's the basic game so obviously we got some erros showing 12 00:01:01.230 --> 00:01:05.580 at the moment because we haven't got the locations and the vocabulary set up so we 13 00:01:05.580 --> 00:01:10.329 need to actually do that we need to come up here initially on line 1 we need to 14 00:01:10.329 --> 00:01:17.799 import shelve of course because we are going to use the shelve module..... 15 00:01:31.590 --> 00:01:42.210 and the other thing we need to do while we remember is down here we ill close 16 00:01:42.210 --> 00:01:49.060 them so....always have to do that 17 00:01:49.060 --> 00:01:55.600 remember, if we are not using with to close things manually which is we have done there 18 00:01:55.600 --> 00:02:00.340 the other change we need to make is on line 6 you notice it currently we got loc equals 19 00:02:00.340 --> 00:02:04.570 one and that's an integer I want to change that so its initialize to a 20 00:02:04.570 --> 00:02:09.399 string because of course if you recall in cave_initilaize.py we 21 00:02:09.399 --> 00:02:14.180 set it up so we will use strings because of course shelves need to use strings 22 00:02:14.180 --> 00:02:18.120 as keys s so because locations is gonna be key ultimately that will need to be 23 00:02:18.120 --> 00:02:23.439 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:23.439 --> 00:02:27.099 is on line 12 we're testing to see whether 0 has been received 25 00:02:27.099 --> 00:02:32.060 because it's a string we need to do a test against a string string comparison and 26 00:02:32.060 --> 00:02:39.260 that's going to be two single quotes to do that comparison now 27 00:02:39.260 --> 00:02:42.189 that should be it so what we'll do is we'll give this a run just to make sure 28 00:02:42.189 --> 00:02:50.220 that does work and I'm going to run this the first time so that, and just check 29 00:02:50.220 --> 00:03:00.290 the I called that he correct name......vocabulary that should be correct we will just 30 00:03:00.290 --> 00:03:07.069 run it again you can see the problem here was in a line 8 so lets checked that out 31 00:03:07.069 --> 00:03:12.469 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:12.469 --> 00:03:16.209 available exits and the problem is if we run it again to check, it is 33 00:03:16.209 --> 00:03:21.709 having a problem with the keys themselves so let's go back to cave_initialize 34 00:03:21.709 --> 00:03:26.569 just to make sure that I haven't messed up something and that seems 35 00:03:26.569 --> 00:03:31.639 that I've made that error again the one I made about three or four times or more in this 36 00:03:31.639 --> 00:03:36.760 course is of course when I ran the initialize hadn't been set up the first 37 00:03:36.760 --> 00:03:40.040 time and I can see that it hasn't been set up so consequently the error we are getting 38 00:03:40.040 --> 00:03:45.970 here when we run these cave_game I'm running now is basically saying well those files 39 00:03:45.970 --> 00:03:49.479 don't exists and the reason they don't exist is of course I didn't run it so 40 00:03:49.479 --> 00:03:53.720 what happened was the challenge code was actually run or actually more likely the 41 00:03:53.720 --> 00:03:57.150 example.py was run and I wasn't really paying attention to the screen 42 00:03:57.150 --> 00:04:01.409 which I should have so what we need to do now is go back to cave_initialize.py 43 00:04:01.409 --> 00:04:07.449 that's correct as it is right click that run cave_initialize that's going to 44 00:04:07.449 --> 00:04:12.620 create a our shelves witch I should have done and then go back to cave_game than 45 00:04:12.620 --> 00:04:21.010 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:21.010 --> 00:04:24.820 and we will do a bit a testing to make sure that it's working 47 00:04:24.820 --> 00:04:31.270 so go north and we're getting an error there and ok int object has no attribute 48 00:04:31.270 --> 00:04:35.230 I think what I've done is I've forgotten to change something in the cave_initialize 49 00:04:35.230 --> 00:04:39.660 so I'll close this down and go back and have a look and actually what I 50 00:04:39.660 --> 00:04:43.890 haven't done which I should have done you notice we are using a key for the locations it 51 00:04:43.890 --> 00:04:49.620 was an integer and I change this correctly to a string but the location 52 00:04:49.620 --> 00:04:55.320 is also used in the exits as you can see in the named exits and I haven't changed 53 00:04:55.320 --> 00:04:59.150 the value to a string now the values has to be string because we gonna be comparing the 54 00:04:59.150 --> 00:05:03.730 string value here to the location we entered there so we could convert 55 00:05:03.730 --> 00:05:07.280 that to an integer but probably the best practices because we are gonna be 56 00:05:07.280 --> 00:05:10.920 continually converting it to get the data right the first time so in that 57 00:05:10.920 --> 00:05:14.750 situation I recommend you do is you changed the data type your going to be using 58 00:05:14.750 --> 00:05:17.680 which in this case is gonna be string so I'm gonna change all of these 59 00:05:17.680 --> 00:05:27.620 to strings...and that should fix the problem once we are done with it 60 00:05:27.620 --> 00:05:40.570 you can see that IntelliJ tries to be helpful 61 00:05:40.570 --> 00:05:45.840 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:45.840 --> 00:05:49.200 wanna put some numbers something in the middle of it so it all a matter of cursor 63 00:05:49.200 --> 00:05:57.920 position to get this right 64 00:05:57.920 --> 00:06:19.530 ....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:19.530 --> 00:06:25.200 hopefully all of them hopefully I didn't miss some strings and actually there is a couple I miss 66 00:06:25.200 --> 00:06:38.310 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:38.310 --> 00:06:43.310 Ok I cannot see any more now so what we're going to do is rerun this we should recreate 68 00:06:43.310 --> 00:06:48.040 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:48.040 --> 00:06:49.070 wasn't working 70 00:06:49.070 --> 00:06:54.350 was we converted the locations two strings here and then in here we had a 71 00:06:54.350 --> 00:06:59.730 location and direction to go to but we also had the location stores integer so 72 00:06:59.730 --> 00:07:03.140 what was happening was there was a string integer comparison taking place 73 00:07:03.140 --> 00:07:09.440 in here so it successfully printed it out but when we try to do a comparison but we ultimately 74 00:07:09.440 --> 00:07:14.630 crash because it was that string and int comparison that is occurring in game so 75 00:07:14.630 --> 00:07:18.720 lets go back to first to cave_initialize right click that and run that to 76 00:07:18.720 --> 00:07:27.630 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:27.630 --> 00:07:39.950 work for us so type north south east west will try typing north that's good and try typing quit 78 00:07:39.950 --> 00:07:40.810 and that is it so 79 00:07:40.810 --> 00:07:46.390 albeit with a bit of bother we got to actually the end and I managed to get this working so 80 00:07:46.390 --> 00:07:51.360 that's good so that was our solution the solution myself and JP came up with so hope you 81 00:07:51.360 --> 00:07:55.220 enjoy that and what was quite interesting I felt was that really there is 82 00:07:55.220 --> 00:07:59.480 very few changes required to the actual game so we swipe out the dictionary 83 00:07:59.480 --> 00:08:00.980 that was in the game 84 00:08:00.980 --> 00:08:07.560 quite easily by replacing the lines 3 and 4, 34 and 35 and we had to little tests 85 00:08:07.560 --> 00:08:11.300 we need to change we need to change the test on line 12 86 00:08:11.300 --> 00:08:16.319 and also change the variable type from an integer to a string on line 6 and of 87 00:08:16.319 --> 00:08:20.729 course the bulk of the changes were in the data in this format where we 88 00:08:20.729 --> 00:08:25.199 actually wrote the correct dictionaries to shelves so this is 89 00:08:25.199 --> 00:08:29.090 what we always recommend you do is get you data correct so don't write the 90 00:08:29.090 --> 00:08:34.459 program to sort of manipulate problems in the data so in other words what 91 00:08:34.459 --> 00:08:37.589 I could have done there was I mention when I was converting these which 92 00:08:37.589 --> 00:08:41.029 should have been two strings by putting the single quotes around them we could 93 00:08:41.029 --> 00:08:44.089 come back to the code here and started doing a comparison and checking 94 00:08:44.089 --> 00:08:48.519 converting integers back to strings and so forth and doing comparison like that 95 00:08:48.519 --> 00:08:53.269 but that would all be sort of coding to cover up the fact that time we made a 96 00:08:53.269 --> 00:08:57.120 mistake with our data by getting the data correct it means we don't have 97 00:08:57.120 --> 00:09:02.290 to do any extra coding in the game and that applies to a lot of coding in general 98 00:09:02.290 --> 00:09:06.959 getting the data correct is a great first step to being able to with a minimal amount of 99 00:09:06.959 --> 00:09:13.269 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:13.269 --> 00:09:17.649 that were needed to get the game working with shelves so that's it 101 00:09:17.649 --> 00:09:22.170 that's the shelve challenge I hope you've enjoyed that so that's actually also the 102 00:09:22.170 --> 00:09:26.470 end of this section so in the next section we gonna be starting to talk 103 00:09:26.470 --> 00:09:30.720 about modules and functions so we look forward to taking you through that and 104 00:09:30.720 --> 00:09:32.050 we'll see you in the next section