1
00:00:05,400 --> 00:00:09,410
so after working through the last set
of videos we've now got a working black

2
00:00:09,410 --> 00:00:12,730
game that we can actually
whatever we want to either from the IDEA

3
00:00:12,730 --> 00:00:17,780
or ultimately from the command line as well
the one thing we can't do is include the

4
00:00:17,780 --> 00:00:20,440
blackjack module in another program

5
00:00:20,440 --> 00:00:23,730
well actually we can but the results
probably not going to be what we wanted

6
00:00:23,730 --> 00:00:28,530
so to see this what I'm going to do  I'm still in my
blackjack project and what

7
00:00:28,530 --> 00:00:34,830
I'm going to do is create another
py file so I'm going to do a new Python file

8
00:00:34,830 --> 00:00:45,020
and will call this one import_test.py we are going to start on an import so....

9
00:00:45,020 --> 00:00:48,400
....and that's the name

10
00:00:48,400 --> 00:00:52,900
of the blackjack.py file there in the
left hand side project section of the

11
00:00:52,900 --> 00:00:57,540
IntelliJ and lets try also doing a print need it to actually have at least

12
00:00:57,540 --> 00:01:06,250
one line of code in their so.... and don't worry

13
00:01:06,250 --> 00:01:09,830
about the __name yet it's just
something to print out but it's also

14
00:01:09,830 --> 00:01:15,030
something that we are going to discuss
shortly so we run the program actually I need to

15
00:01:15,030 --> 00:01:18,800
right click first and make sure I'm running import_test and not running blackjack

16
00:01:18,800 --> 00:01:21,550
because if I have click that that will have run
blackjack which is not what we're

17
00:01:21,550 --> 00:01:25,620
trying to do because we are trying to run this
import_test.py so going to

18
00:01:25,620 --> 00:01:29,540
click on that run import test and you can see
that our blackjack game has actually

19
00:01:29,540 --> 00:01:35,030
started automatically but also note that
our print statements not executed its only

20
00:01:35,030 --> 00:01:39,250
executed when we actually closed down the
window you can see the it come up

21
00:01:39,250 --> 00:01:43,800
__name_ so
this is probably not what we wanted to

22
00:01:43,800 --> 00:01:47,500
happen when we just importing the blackjack module and it's probably not what

23
00:01:47,500 --> 00:01:50,870
you expect it either you would think
that it would just the import would only

24
00:01:50,870 --> 00:01:55,430
input the code and not actually executed
and start the code is certainly in other

25
00:01:55,430 --> 00:01:58,650
parts of the course that we've used the
imports statement it hasn't actually

26
00:01:58,650 --> 00:02:03,160
executed the code its got it ready then we've
had to actually type in some code to

27
00:02:03,160 --> 00:02:06,070
get it to work so clearly
something different is happening with our

28
00:02:06,070 --> 00:02:11,090
blackjack code so I'm gonna discuss why that
happens and what you can do about it so

29
00:02:11,090 --> 00:02:15,170
that you can create your own modules from
your programs and successfully reuse your

30
00:02:15,170 --> 00:02:17,630
functions and classes in other programs

31
00:02:17,630 --> 00:02:22,010
now you may think that all of this is
not really relevant to our blackjack

32
00:02:22,010 --> 00:02:27,280
game program but in fact it actually is
its quite reasonable to include blackjack

33
00:02:27,280 --> 00:02:31,840
for example in a suite of
games you know a compendium games if you like so

34
00:02:31,840 --> 00:02:35,710
the main program with an import each of
the individual games and provided a menu

35
00:02:35,710 --> 00:02:40,520
for the player to select a game to play so
obviously in order to do that we need to

36
00:02:40,520 --> 00:02:44,550
be you know a facility to import the
Blackjack module without it immediately

37
00:02:44,550 --> 00:02:48,600
running so the techniques quite simple
and once I've discuss it will be

38
00:02:48,600 --> 00:02:52,920
writing over future programs along these
lines so that the functions can be made

39
00:02:52,920 --> 00:02:57,440
available to other programs if we want
this is also essential when building up

40
00:02:57,440 --> 00:03:01,030
class libraries in object oriented
programming so it's useful to understand

41
00:03:01,030 --> 00:03:06,790
that before we move into object-oriented
programming in the next section so when

42
00:03:06,790 --> 00:03:11,540
you import a Python module its codes
loaded into memory and then executed

43
00:03:11,540 --> 00:03:16,000
there is a bit more that goes on sorting
out namespaces and other things but

44
00:03:16,000 --> 00:03:20,730
importantly bit here is the code is
executed and that's why our blackjack

45
00:03:20,730 --> 00:03:25,130
game ran when we imported its module and
you can find a complete description of

46
00:03:25,130 --> 00:03:29,390
the import process in the Python
language reference and I'm just gonna bring up this link on the

47
00:03:29,390 --> 00:03:36,100
screen because it can be useful thing for
you to actually check out their is the link on

48
00:03:36,100 --> 00:03:40,810
the screen they're so you can go to the link
and as always its in the Resources section of

49
00:03:40,810 --> 00:03:44,530
the video so what we really want
to do with the blackjack module is only

50
00:03:44,530 --> 00:03:50,300
execute the code if the module is run as a
script which is what the IntelliJ

51
00:03:50,300 --> 00:03:54,380
product this is both IDEA and Pycharm which is their Python

52
00:03:54,380 --> 00:03:59,130
specific IDE which is what they
do if you choose to run the module in also

53
00:03:59,130 --> 00:04:03,440
what Idle does when you run a file
so in the command line you can run your

54
00:04:03,440 --> 00:04:09,670
Python program with something along
these lines if I just got back to the IntelliJ so

55
00:04:09,670 --> 00:04:12,090
this won't work but if
you're running it on the command line you do

56
00:04:12,090 --> 00:04:17,560
something like that.....

57
00:04:17,560 --> 00:04:21,620
...so that's how
you actually kick it off automatically

58
00:04:21,620 --> 00:04:23,510
and as I mentioned

59
00:04:23,510 --> 00:04:28,680
IntelliJ IDEA and Pycharm do something
similar automatically when you choose to

60
00:04:28,680 --> 00:04:33,840
run the modules as you seen numerous times
throughout this course so I'm just gonna delete that

61
00:04:33,840 --> 00:04:38,970
so in all of these cases we want the
code to be executed and the main window

62
00:04:38,970 --> 00:04:42,910
looped to start so that the game can be
played so that's where we're actually

63
00:04:42,910 --> 00:04:48,060
running it when we are choosing to run it however
in this case where we got an import statement

64
00:04:48,060 --> 00:04:51,470
on line 1 we don't want that to
happen we don't want to automatically

65
00:04:51,470 --> 00:04:55,690
start as you saw started we actually just
only want it to run if we actually

66
00:04:55,690 --> 00:05:00,660
specifically are telling the computer to run
it so I mentioned that importing a module

67
00:05:00,660 --> 00:05:05,060
sorts and name spaces as well as
executing the code and one of the

68
00:05:05,060 --> 00:05:09,520
things that happens is that an attribute of
the module called __

69
00:05:09,520 --> 00:05:13,990
name__ you can see
on line three I'm printing it out now is set to be

70
00:05:13,990 --> 00:05:19,650
the name of the module so that is the
file name without path or extension so

71
00:05:19,650 --> 00:05:23,710
when a Python modules executed a script
though the name attribute is set to

72
00:05:23,710 --> 00:05:28,130
__main__ and you can
see that was set there when we actually

73
00:05:28,130 --> 00:05:34,970
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:34,970 --> 00:05:39,690
code runs so what we gonna do is switch
back over to the blackjack py file

75
00:05:39,690 --> 00:05:50,650
...will go to a shuffle method just after
shuffle the last of the methods and just

76
00:05:50,650 --> 00:05:54,900
before the first code that is
executed on line 117 so lets come up here and

77
00:05:54,900 --> 00:06:04,710
we'll type...so I've now put that in the code

78
00:06:06,680 --> 00:06:12,060
so we then run this so we actually running the
blackjack game again just to confirm so run

79
00:06:12,060 --> 00:06:16,280
it we should see the output on the screen
and their is __main and obviously

80
00:06:16,280 --> 00:06:19,580
the game itself is actually running as
well so that's good

81
00:06:19,580 --> 00:06:25,780
however we go back to import_test.py if we actually run that

82
00:06:25,780 --> 00:06:31,430
now you can see we get something different
altogether we get blackjack showing here

83
00:06:31,430 --> 00:06:37,760
this is before the app is run and obviously
once we close it down the we also get the main

84
00:06:37,760 --> 00:06:42,600
showing as well so therefore as a result
of that preventing the code from being

85
00:06:42,600 --> 00:06:46,500
executed when a module is imported is very easy we just have

86
00:06:46,500 --> 00:06:51,340
to check the value of the name attribute
and only execute any execute the code if

87
00:06:51,340 --> 00:06:57,700
it has a value in this case a second
case __main__ so what we can do

88
00:06:57,700 --> 00:07:02,910
is go back to our blackjack.py and make a
change we are going to replace the print statement

89
00:07:02,910 --> 00:07:05,000
that we just added there on line 118

90
00:07:05,000 --> 00:07:13,000
so close this down and we're going to change it and
instead of printing we gonna put....

91
00:07:13,000 --> 00:07:27,780
...and in case the number is also obviously

92
00:07:27,780 --> 00:07:32,220
important here so make sure you get those right
so we are doing a test here to see that we're

93
00:07:32,220 --> 00:07:36,530
actually in the right scope so we do that
test to see whether name is equal to

94
00:07:36,530 --> 00:07:39,640
main and if that's the case with are going to
indent this codes so select all this

95
00:07:39,640 --> 00:07:50,780
code right down and press tab that
indented all that code now so that code

96
00:07:50,780 --> 00:07:57,870
is only executed if that's the case if
__name variable__

97
00:07:57,870 --> 00:08:04,600
is equal to __main__

98
00:08:04,600 --> 00:08:13,960
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:13,960 --> 00:08:16,160
import_test go back

100
00:08:16,160 --> 00:08:19,280
or I could execute it their but if we run it now

101
00:08:19,280 --> 00:08:25,010
we just get main showing up at but if we tab over
you notice that the blackjack game isn't now

102
00:08:25,010 --> 00:08:29,540
starting so that's great it's now
working correctly however we then go back

103
00:08:29,540 --> 00:08:37,770
to blackjack.py itself and actually
run it from within their we find the game is running

104
00:08:37,770 --> 00:08:41,960
and that's because we specifically
said we want to run the game as opposed

105
00:08:41,960 --> 00:08:44,680
to importing it so again the big
distinction there is we're running this

106
00:08:44,680 --> 00:08:51,090
file so we're running this file directly
we are actually telling the IDE IntelliJ

107
00:08:51,090 --> 00:08:55,230
IDEA to execute the code it's being
executed just fins and so therefore the

108
00:08:55,230 --> 00:09:00,110
code on line 118 ends up being true
because we are running in main

109
00:09:00,110 --> 00:09:03,210
coming over to import where we're not
executing something we are only importing it

110
00:09:03,210 --> 00:09:11,320
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:11,320 --> 00:09:14,640
here on line 118 failed
because we weren't running in it main because we

112
00:09:14,640 --> 00:09:18,770
are only importing it so that's great so now we can actually import our code

113
00:09:18,770 --> 00:09:22,700
and not actually execute the code so
that's actually what we wanted but

114
00:09:22,700 --> 00:09:27,310
unfortunately things aren't quite that
simple and because now the blackjack

115
00:09:27,310 --> 00:09:31,390
code is executed we got no way to run
the game once we imported it so we

116
00:09:31,390 --> 00:09:42,910
could try something like.....you could try doing that and run it but that

117
00:09:42,910 --> 00:09:48,920
gives an error and of course the error is
to do with initialization because when we're

118
00:09:48,920 --> 00:09:54,170
running it from there when we importing it
rather all this code from 120 through 178

119
00:09:54,170 --> 00:09:58,910
has not now not being executed
consequently all these variables haven't

120
00:09:58,910 --> 00:10:02,940
been defined so we haven't actually got
a dealer card frame etc so that is why its

121
00:10:02,940 --> 00:10:09,880
crashing so that's actually quite easy to fix all we really have to do is decide which

122
00:10:09,880 --> 00:10:14,520
bits of the blackjack module should be
executed when a module is imported and

123
00:10:14,520 --> 00:10:20,450
moved the test for the __name_ to allow
that to happen so your challenge here

124
00:10:20,450 --> 00:10:26,140
now is to change the blackjack module
to include a function called play that's a

125
00:10:26,140 --> 00:10:29,680
responsible ultimately for allowing or

126
00:10:29,680 --> 00:10:33,960
running the code that starts the game so
you want to test that the play function can be

127
00:10:33,960 --> 00:10:37,980
used to start the game when the module
is imported so again change the

128
00:10:37,980 --> 00:10:42,510
blackjack module itself to include a
function called play and that

129
00:10:42,510 --> 00:10:45,730
function should be responsible for
actually running the code that starts

130
00:10:45,730 --> 00:10:49,250
the game then test the play function can
be used to start the game when the

131
00:10:49,250 --> 00:10:51,770
module is imported that your way of
knowing that things are working as they

132
00:10:51,770 --> 00:10:55,450
should so go away and city and see if you can figure that
out and when you ready to come back

133
00:10:55,450 --> 00:10:58,450
you can come back and check our
solutions so pause the video now....

134
00:11:01,600 --> 00:11:05,140
...ok so hopefully you manage to figure
out a solution for that so again what

135
00:11:05,140 --> 00:11:10,180
we're trying to do is create a function
that's responsible for actually running

136
00:11:10,180 --> 00:11:13,620
the code that starts the game so again the
distinction here is it's running the

137
00:11:13,620 --> 00:11:16,620
code that starts the game it's not the
initialization

138
00:11:17,190 --> 00:11:21,600
go back to blackjack.py the
initialization we still want to actually

139
00:11:21,600 --> 00:11:25,760
execute that at the time it gets imported
but we only want the code that actually

140
00:11:25,760 --> 00:11:31,310
runs the game to be executed when we
specifically called the play function so how do

141
00:11:31,310 --> 00:11:41,140
we do that well we create a new function
we are gonna call it play....and the code is

142
00:11:41,140 --> 00:11:46,550
basically gonna be the code that was in
here I'm gonna take those three or 4 lines of

143
00:11:46,550 --> 00:11:48,440
code their

144
00:11:48,440 --> 00:11:51,990
cut those and put those into the play method

145
00:11:58,420 --> 00:12:05,710
but then the other line of code we want is
the code that starts officially

146
00:12:05,710 --> 00:12:10,000
and that's this code here so we are going to take that out and paste it in

147
00:12:10,000 --> 00:12:19,010
there as well and that is obviously the main loop
that actually runs the game itself so

148
00:12:19,010 --> 00:12:22,320
consequently we've now got a play function
that's really only going to be executed

149
00:12:22,320 --> 00:12:27,200
when we're ready to actually run the game
so we now need to come back here and

150
00:12:27,200 --> 00:12:43,810
remove the indent....then this

151
00:12:43,810 --> 00:12:48,610
bit of code here we are gonna change this now so that name is equal to main if

152
00:12:48,610 --> 00:12:54,120
that's the case we just gonna call the
play method...and that should also be

153
00:12:54,120 --> 00:13:04,070
that the indent removes...so now lets try running this and

154
00:13:04,070 --> 00:13:11,890
see if it actually works and what
we need to do obviously we need to move

155
00:13:11,890 --> 00:13:16,820
this code down here now to be executed
right as the last line we need the rest of

156
00:13:16,820 --> 00:13:20,870
the code to be executed first
and right at the end

157
00:13:21,610 --> 00:13:24,610
after this initialization come down
here

158
00:13:26,310 --> 00:13:31,370
...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:31,370 --> 00:13:35,750
so if we now run this running the blackjack file itself

160
00:13:40,240 --> 00:13:52,690
...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:52,690 --> 00:13:58,380
so close this down so what we've done is we've
added this play function and that's great

162
00:13:58,380 --> 00:13:59,770
its working the first time

163
00:13:59,770 --> 00:14:04,110
we play but if we come up here and have a look at
the function we got this code this

164
00:14:04,110 --> 00:14:08,610
deal_player and the code
that creates or redraws the cards

165
00:14:08,610 --> 00:14:16,310
initially and that needs to be put in
also to our new game method and the new

166
00:14:16,310 --> 00:14:19,830
game method of courses is now executed every time
the new buttons is click just to start a new game

167
00:14:19,830 --> 00:14:26,830
because otherwise that you know when a new game button
is clicked the code is executed the

168
00:14:26,830 --> 00:14:32,200
frames destroyed but there were not
actually creating a new set of cards

169
00:14:32,200 --> 00:14:36,860
for the player and dealers properties so if I click on their and paste that code into be

170
00:14:36,860 --> 00:14:52,510
executed so now if we run this you should
find it works check here and I'll actually

171
00:14:52,510 --> 00:15:00,050
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:15:00,050 --> 00:15:06,170
working correctly because we're getting
a new card new hand each time and hit again

173
00:15:06,170 --> 00:15:12,040
dealer got 21  dealer wins and that's working the score are all working nicely so

174
00:15:12,040 --> 00:15:15,690
that's it that's good so now let's go
back and check to see whether import

175
00:15:15,690 --> 00:15:21,860
_test.py is working and if you
remember we had on new_game

176
00:15:21,860 --> 00:15:24,950
and that's not going to work now because we
created this function now to do it

177
00:15:24,950 --> 00:15:33,960
specifically so its going to be play so if we do blackjack.play it should automatically start are game and if

178
00:15:33,960 --> 00:15:39,130
we come over here and have a look it seems to be
working I'll click on player

179
00:15:39,640 --> 00:15:42,830
and I'll let the dealer go the dealer wins new game

180
00:15:44,100 --> 00:15:49,110
hit dealer dealer wins a game 21 and try it one more time

181
00:15:50,160 --> 00:15:54,630
and player wins so it's working fine and
just as a final check if I comment that line

182
00:15:54,630 --> 00:16:01,700
out and add a exta line to keep Python happy and
run it again we should find the game

183
00:16:01,700 --> 00:16:08,050
now doesn't start automatically click on
and run import test and it correctly doesn't start

184
00:16:08,050 --> 00:16:13,810
because we haven't invoke the play method we will leave that their anyway so that's good that's actually

185
00:16:13,810 --> 00:16:20,740
working now and just as final thing we
could do if we wanted to we could do.....

186
00:16:20,740 --> 00:16:29,160
...and if I do that and run it and obviously

187
00:16:29,160 --> 00:16:33,160
that's not gonna be invoked now until we closed down this so close down that

188
00:16:33,160 --> 00:16:39,750
you can see line of code has been executed
with our list of cards so now the

189
00:16:39,750 --> 00:16:43,020
same pack of cards could be used for
many different types of games now if we wanted to

190
00:16:43,020 --> 00:16:46,820
and that's really another good reason
for copying the cards lists to create

191
00:16:46,820 --> 00:16:50,510
deck in the blackjack game that's why we
actually did the cards list is not

192
00:16:50,510 --> 00:16:54,670
modified once it's been created so it
can be safely reused so this turned out

193
00:16:54,670 --> 00:16:58,640
to not be the easiest program to import and that is because it uses a

194
00:16:58,640 --> 00:17:02,760
graphical user interface that requires a lot
of initialization now there's a lot of

195
00:17:02,760 --> 00:17:06,950
code that has to execute when the module
is imported and the modules also used

196
00:17:06,950 --> 00:17:11,190
many global variables for the same
reason so you can get rid of the global

197
00:17:11,190 --> 00:17:14,730
variables using classes and we'll be
covering that in the next section but

198
00:17:14,730 --> 00:17:18,420
before we get into object-oriented
programming there's a few more aspects

199
00:17:18,420 --> 00:17:23,470
of functions and namespaces that we really want to cover for you understand to finish of

200
00:17:23,470 --> 00:17:27,990
this section so just one last thing
that I think we can add to this before

201
00:17:27,990 --> 00:17:33,720
we actually finish of the blackjack game
and it relates to this code here that I duplicated

202
00:17:33,720 --> 00:17:37,970
so its probably not a good idea we
need to execute this code for dealing

203
00:17:37,970 --> 00:17:41,770
with the player every time dealing new
card for the player one for the dealers and

204
00:17:41,770 --> 00:17:45,720
another for the player each time we are invoking a new game and likewise when we are gonna play

205
00:17:45,720 --> 00:17:51,350
for the first time we need to do that as
well and we don't want to actually put

206
00:17:51,350 --> 00:17:55,590
all this code and only execute the new
game method the first time when we play because

207
00:17:56,350 --> 00:18:00,520
we basically deleting the frame and
re-creating it again so what I'm going to do

208
00:18:00,520 --> 00:18:06,270
is just create one final method and put
this code that is going be executed in two

209
00:18:06,270 --> 00:18:11,320
places in there so that we're not
duplicating our code because it's always a

210
00:18:11,320 --> 00:18:16,830
good idea to fix the issue if your duplicating code so come up here

211
00:18:16,830 --> 00:18:23,070
and I'm going to create a new method so call it....

212
00:18:23,070 --> 00:18:29,720
...and this is going to be our initial
dealing of the cards when a new game

213
00:18:29,720 --> 00:18:40,420
start so grab this code here and I'm gonna
place it in init deal then under the new

214
00:18:40,420 --> 00:18:49,030
game here  and I'm gonna
go....

215
00:18:49,030 --> 00:18:55,970
....

216
00:18:55,970 --> 00:18:56,620
.....

217
00:18:56,620 --> 00:19:01,840
....what's
happening now is that we haven't got any

218
00:19:01,840 --> 00:19:05,670
code duplicated and if by chance we
decided to change the code to d

219
00:19:05,670 --> 00:19:09,730
that initial deal you know maybe we did
something else or showed it or added another

220
00:19:09,730 --> 00:19:14,120
labeled or did something we can still
modified it one place namely the

221
00:19:14,120 --> 00:19:18,200
initial_deal and it's still gonna
work so let's check that does work

222
00:19:18,200 --> 00:19:25,200
I'm actually running import test because its selected
so so that should have started and that's

223
00:19:25,200 --> 00:19:30,040
because we got the code now to play
and just make sure that the game does

224
00:19:30,040 --> 00:19:33,040
work

225
00:19:33,870 --> 00:19:40,320
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:40,320 --> 00:19:45,500
i'm gonna play 19 dealer 18 and I win again i'm on a roll one
more

227
00:19:46,870 --> 00:19:51,210
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:51,210 --> 00:20:00,630
check that and will do one more and notice how
that's work we've got 456789 10 and 11 is 21

229
00:20:00,630 --> 00:20:03,170
but lets actually click another
card even though you wouldn't ordinarily do

230
00:20:03,170 --> 00:20:07,040
that just to make sure the ace going back
to one does work so click on that

231
00:20:07,040 --> 00:20:13,550
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:13,550 --> 00:20:19,830
working on either 11 or 1 depending
on the hands so that's good and click on deal and the

233
00:20:19,830 --> 00:20:23,850
dealer wins this time so that's working okay and
lets do just one final check before we

234
00:20:23,850 --> 00:20:27,320
finish this going to blackjack it will just
run from here

235
00:20:28,270 --> 00:20:42,130
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:42,130 --> 00:20:45,810
I'm going to finish this video here now
and we'll continue on in the next video

