WEBVTT 1 00:00:01.960 --> 00:00:05.970 so it is nearly time for the challenge but before we get into that what we want to do is go 2 00:00:05.970 --> 00:00:09.920 through and have a look at a common source of errors when you're 3 00:00:09.920 --> 00:00:13.790 converting a dictionary that is initialized using a literal into a shelve 4 00:00:13.790 --> 00:00:18.700 know this is one time unfortunately when IntelliJ syntax checking doesn't help in 5 00:00:18.700 --> 00:00:21.910 detecting the error so it is worth being aware of this so you know what to do in 6 00:00:21.910 --> 00:00:25.009 when this situation comes out so what we gonna do is we're going to keep our 7 00:00:25.009 --> 00:00:29.059 recipes but we gonna make the dictionary a little bit more complicated by adding to 8 00:00:29.059 --> 00:00:29.489 it 9 00:00:29.489 --> 00:00:34.760 a dictionary books so will use just two in this case the recipes and the parts needed 10 00:00:34.760 --> 00:00:40.079 for performing maintenance on a motorcycle now because this is a course on Python not motorcycle 11 00:00:40.079 --> 00:00:44.410 maintenance we are gonna keep it a bit simple so a bit of the bike should move 12 00:00:44.410 --> 00:00:48.140 and if it doesn't will apply lubricant if it does move and shouldn't will stick it 13 00:00:48.140 --> 00:00:51.940 down with gahfer tape so here is the dictionary we used to 14 00:00:51.940 --> 00:00:59.820 convert it into so I'll just type that up it is gonna be... 15 00:00:59.820 --> 00:01:10.960 ....and its going to be.... 16 00:01:30.510 --> 00:02:05.030 ... and the last one for this book is pasta.... 17 00:02:05.030 --> 00:02:15.850 and then we are going on to the next book now and the next book is.... 18 00:02:15.850 --> 00:02:27.120 we are going to call that maintenance..... 19 00:02:27.120 --> 00:02:38.170 and..so that is our definition as I said its a 20 00:02:38.170 --> 00:02:42.800 making a bit more complicated by adding a dictionary books now so we got 2 book in 21 00:02:42.800 --> 00:02:47.400 there each with their own individual parts in this case the first one was recipes 22 00:02:47.400 --> 00:02:51.830 the ingredients and the second one was the things needed to fix a motor 23 00:02:51.830 --> 00:02:56.360 bike up or be it very simplistically and just to confirm this works let's try doing a 24 00:02:56.360 --> 00:02:57.920 print.... 25 00:02:57.920 --> 00:03:08.340 ....print out the individual ingredients for soup... 26 00:03:08.340 --> 00:03:15.100 we should be able to do the same.... 27 00:03:15.100 --> 00:03:40.520 ...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:40.520 --> 00:03:49.330 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:49.330 --> 00:03:54.380 butter and milk which are ingredients for scrambled eggs and gaffer tape which was correct for the 30 00:03:54.380 --> 00:03:59.350 loose key which is part of the maintenance key pair so that's good so that is our key value 31 00:03:59.350 --> 00:04:05.520 so that is working fine now the conversion to shelve is quite straightforward so after opening 32 00:04:05.520 --> 00:04:10.970 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:10.970 --> 00:04:15.840 and will do something like this, first is we import the shelve.... 34 00:04:15.840 --> 00:04:29.170 and will put books..... and so once we've done that all we can leave all 35 00:04:29.170 --> 00:04:31.740 the existing prints there because this time it's now going to be printed to 36 00:04:31.740 --> 00:04:36.010 our shelve instead of printing out to the screen so of course the last thing we need 37 00:04:36.010 --> 00:04:41.720 to do after all of that is do a books...and we just need to change it a little bit now 38 00:04:41.720 --> 00:04:45.380 because we've defined books above on line 4 what we need to do is we need to 39 00:04:45.380 --> 00:04:53.580 change this to put books.....and down here what we need to do is we are getting 40 00:04:53.580 --> 00:05:03.380 down to this line and then we've got books here.... 41 00:05:03.380 --> 00:05:17.530 ....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:17.530 --> 00:05:25.140 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:25.140 --> 00:05:31.120 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:31.120 --> 00:05:35.220 that we've got the green tick in the top right hand corner of the screen so as 45 00:05:35.220 --> 00:05:42.200 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:42.200 --> 00:05:45.890 see that the error that comes up is that tuple indices must be integers 47 00:05:45.890 --> 00:05:53.240 or slices and not string or str now the problem is caused over here on line 8 by 48 00:05:53.240 --> 00:05:53.890 leaving a 49 00:05:53.890 --> 00:05:57.600 comma at the end of that line now this is a very easy mistake to make when 50 00:05:57.600 --> 00:06:01.440 converting a literal dictionary into individual shelve assignments and what 51 00:06:01.440 --> 00:06:04.790 we've actually ended up is a value to the key recipes that is a 52 00:06:04.790 --> 00:06:05.390 tuple 53 00:06:05.390 --> 00:06:09.370 consisting of a dictionary and an empty entry and we can see that by changing 54 00:06:09.370 --> 00:06:20.200 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:20.200 --> 00:06:26.200 for soup like that after removing the comma it shouldn't have been there if we run it again 56 00:06:26.200 --> 00:06:36.760 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:36.760 --> 00:06:43.400 it's really quite important when your converting a dictionary into a shelve so keep that in mind we really 58 00:06:43.400 --> 00:06:46.740 need to take care to ensure that there's no additional commas or curly brackets 59 00:06:46.740 --> 00:06:51.730 where you will get odd results and error that are sometimes can be hard to debug so do 60 00:06:51.730 --> 00:06:55.080 keep that in mind when using shelve to convert it as it was shown in this 61 00:06:55.080 --> 00:06:59.960 example ok so on with the challenge so what I'm going to do now is create a new 62 00:06:59.960 --> 00:07:09.970 file so I'm gonna call this challenge and although we did say a shelve perhaps not the best choice for 63 00:07:09.970 --> 00:07:14.400 persisting data in an adventure game we have set up fairly complex dictionaries in 64 00:07:14.400 --> 00:07:15.030 this game 65 00:07:15.030 --> 00:07:18.550 and as is its structure that you'll understand because we used it we're 66 00:07:18.550 --> 00:07:21.660 going to use that so you can focus on the code changes rather than trying to 67 00:07:21.660 --> 00:07:24.950 understand yet another data structure so this challenge will be good practice for 68 00:07:24.950 --> 00:07:29.760 switching dictionary to a shelve and also vice versa should you need to but rest 69 00:07:29.760 --> 00:07:34.100 assured with that said that will not be using shelves to persists the basic game data as 70 00:07:34.100 --> 00:07:36.830 we complete the game I'm gonna paste in and let you know 71 00:07:36.830 --> 00:07:44.360 to show you what the challenges is ok so here is the challenge and I've paste some code in 72 00:07:44.360 --> 00:07:47.510 from a previous challenge so what you need to do is modify the program from 73 00:07:47.510 --> 00:07:51.300 the second dictionary challenge of of lecture 56 and that's what these code 74 00:07:51.300 --> 00:07:55.930 is from to use shelves instead of the dictionaries that we of course used in that challenge 75 00:07:55.930 --> 00:08:00.650 and you want to do this by creating two programs first one should be called cave 76 00:08:00.650 --> 00:08:05.050 underscore initialize dot py and that should create the two shelves which are 77 00:08:05.050 --> 00:08:09.590 locations and vocabulary with the appropriate keys and values and second 78 00:08:09.590 --> 00:08:13.520 file that you want to create is called cave underscore game dot py and that 79 00:08:13.520 --> 00:08:17.930 will then used the two shelves instead of using dictionaries so apart from opening 80 00:08:17.930 --> 00:08:19.520 and closing the shelves 81 00:08:19.520 --> 00:08:24.470 cave_game would need only to change to the actual code remember that shelve keys 82 00:08:24.470 --> 00:08:29.260 must be strings so that's very important to keep in mind and just to be clear the 83 00:08:29.260 --> 00:08:33.080 cave_game py will contain the code from line 45 on words below so in other 84 00:08:33.080 --> 00:08:37.360 words the code starting with this loc = 1 through to the end everything before 85 00:08:37.360 --> 00:08:43.740 that as you can see here on lines 14 through 43 are inclusive that ultimately gonna be modified 86 00:08:43.740 --> 00:08:47.780 to use shelve being cave_initialize.py so 87 00:08:47.780 --> 00:08:52.010 essentially the process will be that you will run cave_initialized.py and that will 88 00:08:52.010 --> 00:08:56.710 create the necessary shelves then of course in the cave_game.py that 89 00:08:56.710 --> 00:09:00.870 is gonna be using those shelves and that where you actually run the 90 00:09:00.870 --> 00:09:05.010 game itself so that's the challenge now is the time to pause the video and go away 91 00:09:05.010 --> 00:09:08.990 and see if you can figure out that challenge and when you done come back and you can 92 00:09:08.990 --> 00:09:16.950 compare what you come up with our solution so we'll see when you get back 93 00:09:16.950 --> 00:09:25.080 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:25.080 --> 00:09:33.580 a copy of that and I'm going to create the new Python file and call that cave_initialized.py 95 00:09:33.580 --> 00:09:38.880 and that's going to be our initialization code obviously we got the code here that 96 00:09:38.880 --> 00:09:42.240 we're going to be reliant on and that does need to be changed so we're going to do is 97 00:09:42.240 --> 00:09:45.360 I'm going to just take that code so you can see what I'm doing to change it 98 00:09:45.360 --> 00:09:51.390 the code up to an including line 43 I'm gonna paste that in 99 00:09:51.390 --> 00:09:58.270 two our cave_initialize.py then I'm going to go back up to line 1 and start making some 100 00:09:58.270 --> 00:10:06.520 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:06.520 --> 00:10:18.240 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:18.240 --> 00:10:24.350 need to use strings as keys for a shelve and obviously as you can see right now this 103 00:10:24.350 --> 00:10:29.120 is not the case we're not using strings there we are using entered 104 00:10:29.120 --> 00:10:33.020 integers so a way to changes could be to changes this code a little bit so we could 105 00:10:33.020 --> 00:10:41.240 put locations...put the location in as a string as you can see there and 106 00:10:41.240 --> 00:10:43.010 set that to equals 107 00:10:43.010 --> 00:10:49.050 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:49.050 --> 00:10:58.970 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:10:58.970 --> 00:11:14.110 back to the start of the line paste that in, this is location 1...and the next one 110 00:11:14.110 --> 00:11:20.730 location 2...and I'm deleting the commas as you can see each time, remember we talked about 111 00:11:20.730 --> 00:11:40.769 that earlier in the video how that's important location 3....and 4....and the last one obviously 112 00:11:40.769 --> 00:11:52.410 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:52.410 --> 00:12:05.130 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:05.130 --> 00:12:09.060 that is the first part and we need to do the similar thing now for second part 115 00:12:09.060 --> 00:12:13.640 obviously the second part vocabulary is using a key currently a string and not 116 00:12:13.640 --> 00:12:17.269 as an integer but we still need to convert it into that format so doing 117 00:12:17.269 --> 00:12:29.899 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:29.899 --> 00:12:36.519 vocabulary...will copy that part as we did before and I'm going to 119 00:12:36.519 --> 00:13:01.589 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:01.589 --> 00:13:10.870 copy and paste.... 121 00:13:10.870 --> 00:13:58.970 ..and next one is east...and next one is west....and road...hill... 122 00:13:58.970 --> 00:14:45.980 ..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:45.980 --> 00:14:48.170 having to shelves opening at once not that it really matters 124 00:14:48.170 --> 00:14:53.199 but it is good practice to have all the code in some areas so we are opening the shelve here 125 00:14:53.199 --> 00:14:57.579 we're performing the operation when want then we close it immediately and likewise with vocabulary 126 00:14:57.579 --> 00:15:00.250 add some spaces so its a bit more readable 127 00:15:00.250 --> 00:15:06.220 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:06.220 --> 00:15:09.980 and we should have a green tick in the top right hand corner which we've got so that's good 129 00:15:09.980 --> 00:15:14.519 so just a recap what we had to do was because shelve keys have to be strings which we 130 00:15:14.519 --> 00:15:18.610 talked about we had to change all the numeric entries representing the locations 131 00:15:18.610 --> 00:15:23.610 firstly two strings and you can see they are all done now on lines 5 to 25 and then 132 00:15:23.610 --> 00:15:28.079 we did a similar thing for the vocabulary even though they were already strings 133 00:15:28.079 --> 00:15:32.259 and not integers we still just change the format to make it a bit easier and a bit 134 00:15:32.259 --> 00:15:35.550 more consistent so at this point we're actually done so we've got the 135 00:15:35.550 --> 00:15:39.129 initialization down cave_initialized.py in the next video 136 00:15:39.129 --> 00:15:43.170 will come back and we'll make the changes to cav_game so that 137 00:15:43.170 --> 00:15:44.980 we can get that working so see you in the next video