WEBVTT 1 00:00:01.960 --> 00:00:05.970 so after working through the last set of videos we've now got a working black 2 00:00:05.970 --> 00:00:09.299 game that we can actually whatever we want to either from the IDEA 3 00:00:09.299 --> 00:00:14.349 or ultimately from the command line as well the one thing we can't do is include the 4 00:00:14.349 --> 00:00:17.009 blackjack module in another program 5 00:00:17.009 --> 00:00:20.290 well actually we can but the results probably not going to be what we wanted 6 00:00:20.290 --> 00:00:25.090 so to see this what I'm going to do I'm still in my blackjack project and what 7 00:00:25.090 --> 00:00:31.390 I'm going to do is create another py file so I'm going to do a new Python file 8 00:00:31.390 --> 00:00:41.589 and will call this one import_test.py we are going to start on an import so.... 9 00:00:41.589 --> 00:00:44.969 ....and that's the name 10 00:00:44.969 --> 00:00:49.460 of the blackjack.py file there in the left hand side project section of the 11 00:00:49.460 --> 00:00:54.100 IntelliJ and lets try also doing a print need it to actually have at least 12 00:00:54.100 --> 00:01:02.819 one line of code in their so.... and don't worry 13 00:01:02.819 --> 00:01:06.399 about the __name yet it's just something to print out but it's also 14 00:01:06.399 --> 00:01:11.590 something that we are going to discuss shortly so we run the program actually I need to 15 00:01:11.590 --> 00:01:15.369 right click first and make sure I'm running import_test and not running blackjack 16 00:01:15.369 --> 00:01:18.110 because if I have click that that will have run blackjack which is not what we're 17 00:01:18.110 --> 00:01:22.189 trying to do because we are trying to run this import_test.py so going to 18 00:01:22.189 --> 00:01:26.100 click on that run import test and you can see that our blackjack game has actually 19 00:01:26.100 --> 00:01:31.590 started automatically but also note that our print statements not executed its only 20 00:01:31.590 --> 00:01:35.810 executed when we actually closed down the window you can see the it come up 21 00:01:35.810 --> 00:01:40.369 __name_ so this is probably not what we wanted to 22 00:01:40.369 --> 00:01:44.060 happen when we just importing the blackjack module and it's probably not what 23 00:01:44.060 --> 00:01:47.439 you expect it either you would think that it would just the import would only 24 00:01:47.439 --> 00:01:51.990 input the code and not actually executed and start the code is certainly in other 25 00:01:51.990 --> 00:01:55.219 parts of the course that we've used the imports statement it hasn't actually 26 00:01:55.219 --> 00:01:59.729 executed the code its got it ready then we've had to actually type in some code to 27 00:01:59.729 --> 00:02:02.630 get it to work so clearly something different is happening with our 28 00:02:02.630 --> 00:02:07.659 blackjack code so I'm gonna discuss why that happens and what you can do about it so 29 00:02:07.659 --> 00:02:11.730 that you can create your own modules from your programs and successfully reuse your 30 00:02:11.730 --> 00:02:14.190 functions and classes in other programs 31 00:02:14.190 --> 00:02:18.570 now you may think that all of this is not really relevant to our blackjack 32 00:02:18.570 --> 00:02:23.840 game program but in fact it actually is its quite reasonable to include blackjack 33 00:02:23.840 --> 00:02:28.400 for example in a suite of games you know a compendium games if you like so 34 00:02:28.400 --> 00:02:32.270 the main program with an import each of the individual games and provided a menu 35 00:02:32.270 --> 00:02:37.080 for the player to select a game to play so obviously in order to do that we need to 36 00:02:37.080 --> 00:02:41.110 be you know a facility to import the Blackjack module without it immediately 37 00:02:41.110 --> 00:02:45.160 running so the techniques quite simple and once I've discuss it will be 38 00:02:45.160 --> 00:02:49.480 writing over future programs along these lines so that the functions can be made 39 00:02:49.480 --> 00:02:54.000 available to other programs if we want this is also essential when building up 40 00:02:54.000 --> 00:02:57.590 class libraries in object oriented programming so it's useful to understand 41 00:02:57.590 --> 00:03:03.350 that before we move into object-oriented programming in the next section so when 42 00:03:03.350 --> 00:03:08.100 you import a Python module its codes loaded into memory and then executed 43 00:03:08.100 --> 00:03:12.560 there is a bit more that goes on sorting out namespaces and other things but 44 00:03:12.560 --> 00:03:17.290 importantly bit here is the code is executed and that's why our blackjack 45 00:03:17.290 --> 00:03:21.690 game ran when we imported its module and you can find a complete description of 46 00:03:21.690 --> 00:03:25.959 the import process in the Python language reference and I'm just gonna bring up this link on the 47 00:03:25.959 --> 00:03:32.660 screen because it can be useful thing for you to actually check out their is the link on 48 00:03:32.660 --> 00:03:37.370 the screen they're so you can go to the link and as always its in the Resources section of 49 00:03:37.370 --> 00:03:41.090 the video so what we really want to do with the blackjack module is only 50 00:03:41.090 --> 00:03:46.860 execute the code if the module is run as a script which is what the IntelliJ 51 00:03:46.860 --> 00:03:50.940 product this is both IDEA and Pycharm which is their Python 52 00:03:50.940 --> 00:03:55.690 specific IDE which is what they do if you choose to run the module in also 53 00:03:55.690 --> 00:04:00.000 what Idle does when you run a file so in the command line you can run your 54 00:04:00.000 --> 00:04:06.230 Python program with something along these lines if I just got back to the IntelliJ so 55 00:04:06.230 --> 00:04:08.650 this won't work but if you're running it on the command line you do 56 00:04:08.650 --> 00:04:14.120 something like that..... 57 00:04:14.120 --> 00:04:18.180 ...so that's how you actually kick it off automatically 58 00:04:18.180 --> 00:04:20.079 and as I mentioned 59 00:04:20.079 --> 00:04:25.240 IntelliJ IDEA and Pycharm do something similar automatically when you choose to 60 00:04:25.240 --> 00:04:30.400 run the modules as you seen numerous times throughout this course so I'm just gonna delete that 61 00:04:30.400 --> 00:04:35.530 so in all of these cases we want the code to be executed and the main window 62 00:04:35.530 --> 00:04:39.479 looped to start so that the game can be played so that's where we're actually 63 00:04:39.479 --> 00:04:44.629 running it when we are choosing to run it however in this case where we got an import statement 64 00:04:44.629 --> 00:04:48.039 on line 1 we don't want that to happen we don't want to automatically 65 00:04:48.039 --> 00:04:52.259 start as you saw started we actually just only want it to run if we actually 66 00:04:52.259 --> 00:04:57.229 specifically are telling the computer to run it so I mentioned that importing a module 67 00:04:57.229 --> 00:05:01.629 sorts and name spaces as well as executing the code and one of the 68 00:05:01.629 --> 00:05:06.080 things that happens is that an attribute of the module called __ 69 00:05:06.080 --> 00:05:10.550 name__ you can see on line three I'm printing it out now is set to be 70 00:05:10.550 --> 00:05:16.210 the name of the module so that is the file name without path or extension so 71 00:05:16.210 --> 00:05:20.279 when a Python modules executed a script though the name attribute is set to 72 00:05:20.279 --> 00:05:24.699 __main__ and you can see that was set there when we actually 73 00:05:24.699 --> 00:05:31.539 printed this out over here in our output window and we can see this by printing an attribute before any of our 74 00:05:31.539 --> 00:05:36.259 code runs so what we gonna do is switch back over to the blackjack py file 75 00:05:36.259 --> 00:05:47.219 ...will go to a shuffle method just after shuffle the last of the methods and just 76 00:05:47.219 --> 00:05:51.460 before the first code that is executed on line 117 so lets come up here and 77 00:05:51.460 --> 00:06:01.270 we'll type...so I've now put that in the code 78 00:06:03.249 --> 00:06:08.629 so we then run this so we actually running the blackjack game again just to confirm so run 79 00:06:08.629 --> 00:06:12.849 it we should see the output on the screen and their is __main and obviously 80 00:06:12.849 --> 00:06:16.149 the game itself is actually running as well so that's good 81 00:06:16.149 --> 00:06:22.349 however we go back to import_test.py if we actually run that 82 00:06:22.349 --> 00:06:27.999 now you can see we get something different altogether we get blackjack showing here 83 00:06:27.999 --> 00:06:34.329 this is before the app is run and obviously once we close it down the we also get the main 84 00:06:34.329 --> 00:06:39.169 showing as well so therefore as a result of that preventing the code from being 85 00:06:39.169 --> 00:06:43.069 executed when a module is imported is very easy we just have 86 00:06:43.069 --> 00:06:47.909 to check the value of the name attribute and only execute any execute the code if 87 00:06:47.909 --> 00:06:54.269 it has a value in this case a second case __main__ so what we can do 88 00:06:54.269 --> 00:06:59.479 is go back to our blackjack.py and make a change we are going to replace the print statement 89 00:06:59.479 --> 00:07:01.569 that we just added there on line 118 90 00:07:01.569 --> 00:07:09.569 so close this down and we're going to change it and instead of printing we gonna put.... 91 00:07:09.569 --> 00:07:24.349 ...and in case the number is also obviously 92 00:07:24.349 --> 00:07:28.789 important here so make sure you get those right so we are doing a test here to see that we're 93 00:07:28.789 --> 00:07:33.099 actually in the right scope so we do that test to see whether name is equal to 94 00:07:33.099 --> 00:07:36.209 main and if that's the case with are going to indent this codes so select all this 95 00:07:36.209 --> 00:07:47.349 code right down and press tab that indented all that code now so that code 96 00:07:47.349 --> 00:07:54.439 is only executed if that's the case if __name variable__ 97 00:07:54.439 --> 00:08:01.169 is equal to __main__ 98 00:08:01.169 --> 00:08:10.529 ok so that's that and fix that up to keep IntelliJ happy so now if we come back and we are in 99 00:08:10.529 --> 00:08:12.729 import_test go back 100 00:08:12.729 --> 00:08:15.840 or I could execute it their but if we run it now 101 00:08:15.840 --> 00:08:21.570 we just get main showing up at but if we tab over you notice that the blackjack game isn't now 102 00:08:21.570 --> 00:08:26.100 starting so that's great it's now working correctly however we then go back 103 00:08:26.100 --> 00:08:34.330 to blackjack.py itself and actually run it from within their we find the game is running 104 00:08:34.330 --> 00:08:38.520 and that's because we specifically said we want to run the game as opposed 105 00:08:38.520 --> 00:08:41.240 to importing it so again the big distinction there is we're running this 106 00:08:41.240 --> 00:08:47.650 file so we're running this file directly we are actually telling the IDE IntelliJ 107 00:08:47.650 --> 00:08:51.790 IDEA to execute the code it's being executed just fins and so therefore the 108 00:08:51.790 --> 00:08:56.670 code on line 118 ends up being true because we are running in main 109 00:08:56.670 --> 00:08:59.779 coming over to import where we're not executing something we are only importing it 110 00:08:59.779 --> 00:09:07.880 and when we got to run that we are only getting the print out on line three and that is because this test 111 00:09:07.880 --> 00:09:11.200 here on line 118 failed because we weren't running in it main because we 112 00:09:11.200 --> 00:09:15.339 are only importing it so that's great so now we can actually import our code 113 00:09:15.339 --> 00:09:19.260 and not actually execute the code so that's actually what we wanted but 114 00:09:19.260 --> 00:09:23.870 unfortunately things aren't quite that simple and because now the blackjack 115 00:09:23.870 --> 00:09:27.959 code is executed we got no way to run the game once we imported it so we 116 00:09:27.959 --> 00:09:39.470 could try something like.....you could try doing that and run it but that 117 00:09:39.470 --> 00:09:45.480 gives an error and of course the error is to do with initialization because when we're 118 00:09:45.480 --> 00:09:50.730 running it from there when we importing it rather all this code from 120 through 178 119 00:09:50.730 --> 00:09:55.470 has not now not being executed consequently all these variables haven't 120 00:09:55.470 --> 00:09:59.500 been defined so we haven't actually got a dealer card frame etc so that is why its 121 00:09:59.500 --> 00:10:06.440 crashing so that's actually quite easy to fix all we really have to do is decide which 122 00:10:06.440 --> 00:10:11.089 bits of the blackjack module should be executed when a module is imported and 123 00:10:11.089 --> 00:10:17.010 moved the test for the __name_ to allow that to happen so your challenge here 124 00:10:17.010 --> 00:10:22.709 now is to change the blackjack module to include a function called play that's a 125 00:10:22.709 --> 00:10:26.240 responsible ultimately for allowing or 126 00:10:26.240 --> 00:10:30.529 running the code that starts the game so you want to test that the play function can be 127 00:10:30.529 --> 00:10:34.540 used to start the game when the module is imported so again change the 128 00:10:34.540 --> 00:10:39.070 blackjack module itself to include a function called play and that 129 00:10:39.070 --> 00:10:42.290 function should be responsible for actually running the code that starts 130 00:10:42.290 --> 00:10:45.810 the game then test the play function can be used to start the game when the 131 00:10:45.810 --> 00:10:48.330 module is imported that your way of knowing that things are working as they 132 00:10:48.330 --> 00:10:52.010 should so go away and city and see if you can figure that out and when you ready to come back 133 00:10:52.010 --> 00:10:55.010 you can come back and check our solutions so pause the video now.... 134 00:10:58.160 --> 00:11:01.709 ...ok so hopefully you manage to figure out a solution for that so again what 135 00:11:01.709 --> 00:11:06.740 we're trying to do is create a function that's responsible for actually running 136 00:11:06.740 --> 00:11:10.180 the code that starts the game so again the distinction here is it's running the 137 00:11:10.180 --> 00:11:13.180 code that starts the game it's not the initialization 138 00:11:13.750 --> 00:11:18.160 go back to blackjack.py the initialization we still want to actually 139 00:11:18.160 --> 00:11:22.329 execute that at the time it gets imported but we only want the code that actually 140 00:11:22.329 --> 00:11:27.870 runs the game to be executed when we specifically called the play function so how do 141 00:11:27.870 --> 00:11:37.709 we do that well we create a new function we are gonna call it play....and the code is 142 00:11:37.709 --> 00:11:43.110 basically gonna be the code that was in here I'm gonna take those three or 4 lines of 143 00:11:43.110 --> 00:11:45.000 code their 144 00:11:45.000 --> 00:11:48.550 cut those and put those into the play method 145 00:11:54.980 --> 00:12:02.279 but then the other line of code we want is the code that starts officially 146 00:12:02.279 --> 00:12:06.560 and that's this code here so we are going to take that out and paste it in 147 00:12:06.560 --> 00:12:15.570 there as well and that is obviously the main loop that actually runs the game itself so 148 00:12:15.570 --> 00:12:18.889 consequently we've now got a play function that's really only going to be executed 149 00:12:18.889 --> 00:12:23.760 when we're ready to actually run the game so we now need to come back here and 150 00:12:23.760 --> 00:12:40.370 remove the indent....then this 151 00:12:40.370 --> 00:12:45.170 bit of code here we are gonna change this now so that name is equal to main if 152 00:12:45.170 --> 00:12:50.680 that's the case we just gonna call the play method...and that should also be 153 00:12:50.680 --> 00:13:00.639 that the indent removes...so now lets try running this and 154 00:13:00.639 --> 00:13:08.459 see if it actually works and what we need to do obviously we need to move 155 00:13:08.459 --> 00:13:13.380 this code down here now to be executed right as the last line we need the rest of 156 00:13:13.380 --> 00:13:17.430 the code to be executed first and right at the end 157 00:13:18.170 --> 00:13:21.170 after this initialization come down here 158 00:13:22.870 --> 00:13:27.930 ...we put the code their and we can remove the new game as well at that point so at this point now it should actually work 159 00:13:27.930 --> 00:13:32.310 so if we now run this running the blackjack file itself 160 00:13:36.800 --> 00:13:49.250 ...and click on player to get another card now we are busted click on new and we got a problem here we got no dealer card and I think I know what that is... 161 00:13:49.250 --> 00:13:54.940 so close this down so what we've done is we've added this play function and that's great 162 00:13:54.940 --> 00:13:56.339 its working the first time 163 00:13:56.339 --> 00:14:00.670 we play but if we come up here and have a look at the function we got this code this 164 00:14:00.670 --> 00:14:05.170 deal_player and the code that creates or redraws the cards 165 00:14:05.170 --> 00:14:12.870 initially and that needs to be put in also to our new game method and the new 166 00:14:12.870 --> 00:14:16.390 game method of courses is now executed every time the new buttons is click just to start a new game 167 00:14:16.390 --> 00:14:23.399 because otherwise that you know when a new game button is clicked the code is executed the 168 00:14:23.399 --> 00:14:28.760 frames destroyed but there were not actually creating a new set of cards 169 00:14:28.760 --> 00:14:33.420 for the player and dealers properties so if I click on their and paste that code into be 170 00:14:33.420 --> 00:14:49.079 executed so now if we run this you should find it works check here and I'll actually 171 00:14:49.079 --> 00:14:56.610 hit we got 21 thats good click on dealer dealer losses so I win click on new game and we now got it 172 00:14:56.610 --> 00:15:02.730 working correctly because we're getting a new card new hand each time and hit again 173 00:15:02.730 --> 00:15:08.600 dealer got 21 dealer wins and that's working the score are all working nicely so 174 00:15:08.600 --> 00:15:12.250 that's it that's good so now let's go back and check to see whether import 175 00:15:12.250 --> 00:15:18.420 _test.py is working and if you remember we had on new_game 176 00:15:18.420 --> 00:15:21.510 and that's not going to work now because we created this function now to do it 177 00:15:21.510 --> 00:15:30.520 specifically so its going to be play so if we do blackjack.play it should automatically start are game and if 178 00:15:30.520 --> 00:15:35.690 we come over here and have a look it seems to be working I'll click on player 179 00:15:36.209 --> 00:15:39.390 and I'll let the dealer go the dealer wins new game 180 00:15:40.660 --> 00:15:45.670 hit dealer dealer wins a game 21 and try it one more time 181 00:15:46.720 --> 00:15:51.190 and player wins so it's working fine and just as a final check if I comment that line 182 00:15:51.190 --> 00:15:58.269 out and add a exta line to keep Python happy and run it again we should find the game 183 00:15:58.269 --> 00:16:04.610 now doesn't start automatically click on and run import test and it correctly doesn't start 184 00:16:04.610 --> 00:16:10.379 because we haven't invoke the play method we will leave that their anyway so that's good that's actually 185 00:16:10.379 --> 00:16:17.300 working now and just as final thing we could do if we wanted to we could do..... 186 00:16:17.300 --> 00:16:25.720 ...and if I do that and run it and obviously 187 00:16:25.720 --> 00:16:29.720 that's not gonna be invoked now until we closed down this so close down that 188 00:16:29.720 --> 00:16:36.310 you can see line of code has been executed with our list of cards so now the 189 00:16:36.310 --> 00:16:39.589 same pack of cards could be used for many different types of games now if we wanted to 190 00:16:39.589 --> 00:16:43.389 and that's really another good reason for copying the cards lists to create 191 00:16:43.389 --> 00:16:47.079 deck in the blackjack game that's why we actually did the cards list is not 192 00:16:47.079 --> 00:16:51.230 modified once it's been created so it can be safely reused so this turned out 193 00:16:51.230 --> 00:16:55.209 to not be the easiest program to import and that is because it uses a 194 00:16:55.209 --> 00:16:59.329 graphical user interface that requires a lot of initialization now there's a lot of 195 00:16:59.329 --> 00:17:03.519 code that has to execute when the module is imported and the modules also used 196 00:17:03.519 --> 00:17:07.750 many global variables for the same reason so you can get rid of the global 197 00:17:07.750 --> 00:17:11.299 variables using classes and we'll be covering that in the next section but 198 00:17:11.299 --> 00:17:14.980 before we get into object-oriented programming there's a few more aspects 199 00:17:14.980 --> 00:17:20.030 of functions and namespaces that we really want to cover for you understand to finish of 200 00:17:20.030 --> 00:17:24.559 this section so just one last thing that I think we can add to this before 201 00:17:24.559 --> 00:17:30.280 we actually finish of the blackjack game and it relates to this code here that I duplicated 202 00:17:30.280 --> 00:17:34.530 so its probably not a good idea we need to execute this code for dealing 203 00:17:34.530 --> 00:17:38.330 with the player every time dealing new card for the player one for the dealers and 204 00:17:38.330 --> 00:17:42.289 another for the player each time we are invoking a new game and likewise when we are gonna play 205 00:17:42.289 --> 00:17:47.919 for the first time we need to do that as well and we don't want to actually put 206 00:17:47.919 --> 00:17:52.159 all this code and only execute the new game method the first time when we play because 207 00:17:52.910 --> 00:17:57.080 we basically deleting the frame and re-creating it again so what I'm going to do 208 00:17:57.080 --> 00:18:02.830 is just create one final method and put this code that is going be executed in two 209 00:18:02.830 --> 00:18:07.880 places in there so that we're not duplicating our code because it's always a 210 00:18:07.880 --> 00:18:13.390 good idea to fix the issue if your duplicating code so come up here 211 00:18:13.390 --> 00:18:19.630 and I'm going to create a new method so call it.... 212 00:18:19.630 --> 00:18:26.280 ...and this is going to be our initial dealing of the cards when a new game 213 00:18:26.280 --> 00:18:36.980 start so grab this code here and I'm gonna place it in init deal then under the new 214 00:18:36.980 --> 00:18:45.590 game here and I'm gonna go.... 215 00:18:45.590 --> 00:18:52.530 .... 216 00:18:52.530 --> 00:18:53.180 ..... 217 00:18:53.180 --> 00:18:58.400 ....what's happening now is that we haven't got any 218 00:18:58.400 --> 00:19:02.230 code duplicated and if by chance we decided to change the code to d 219 00:19:02.230 --> 00:19:06.290 that initial deal you know maybe we did something else or showed it or added another 220 00:19:06.290 --> 00:19:10.680 labeled or did something we can still modified it one place namely the 221 00:19:10.680 --> 00:19:14.760 initial_deal and it's still gonna work so let's check that does work 222 00:19:14.760 --> 00:19:21.760 I'm actually running import test because its selected so so that should have started and that's 223 00:19:21.760 --> 00:19:26.600 because we got the code now to play and just make sure that the game does 224 00:19:26.600 --> 00:19:29.600 work 225 00:19:30.430 --> 00:19:36.880 we got 20's so I'm going to click on deal and i win that click on new game and we got cards looks like its going to work 226 00:19:36.880 --> 00:19:42.060 i'm gonna play 19 dealer 18 and I win again i'm on a roll one more 227 00:19:43.430 --> 00:19:47.770 15 actually will just try another one and will just see whether the ace does work I think we saw that working but lets just 228 00:19:47.770 --> 00:19:57.190 check that and will do one more and notice how that's work we've got 456789 10 and 11 is 21 229 00:19:57.190 --> 00:19:59.730 but lets actually click another card even though you wouldn't ordinarily do 230 00:19:59.730 --> 00:20:03.600 that just to make sure the ace going back to one does work so click on that 231 00:20:03.600 --> 00:20:10.110 and you can see that does work 6 3 9 3 is 12 4 is 16 1 is 17 so we definitely got the ace 232 00:20:10.110 --> 00:20:16.390 working on either 11 or 1 depending on the hands so that's good and click on deal and the 233 00:20:16.390 --> 00:20:20.410 dealer wins this time so that's working okay and lets do just one final check before we 234 00:20:20.410 --> 00:20:23.880 finish this going to blackjack it will just run from here 235 00:20:24.830 --> 00:20:38.690 to confirm that it still works that way and that is still working so click on dealer and player wins alright so 236 00:20:38.690 --> 00:20:42.370 I'm going to finish this video here now and we'll continue on in the next video