1
00:00:05,340 --> 00:00:10,120
ok so moving on with our blackjack
program now just before we continue their is a

2
00:00:10,120 --> 00:00:16,200
few items of housekeeping that
I want to go through firstly when we actually run

3
00:00:16,200 --> 00:00:20,660
the app I've mentioned before that it wasn't
sort of looking the best so if I have

4
00:00:20,660 --> 00:00:25,680
look and run it you can see that there's a lot of
white space and obviously as we start

5
00:00:25,680 --> 00:00:28,680
using it it gets better and the cards get
dealt

6
00:00:29,340 --> 00:00:33,600
and its starting to take up some space so
probably the easiest thing to fix right

7
00:00:33,600 --> 00:00:36,650
now is just a change this backgrounds so what
I'm going to do is change the background

8
00:00:36,650 --> 00:00:39,840
so its green so that's number one

9
00:00:39,840 --> 00:00:45,360
go ahead and do and where I would do that
would be just under the two lines that

10
00:00:45,360 --> 00:00:51,870
set the window title and the geometry
now here I'm gonna type....

11
00:00:51,870 --> 00:01:06,430
...and if we run that we've got a green window that's going to look

12
00:01:06,430 --> 00:01:11,160
a little bit better once we've got some cards in there so that's number one that has made our application

13
00:01:11,160 --> 00:01:13,730
look a bit nicer and bearing in mind that we haven't

14
00:01:13,730 --> 00:01:20,380
got an text showing here but that will be becoming
but the other issue that I want to fix

15
00:01:20,380 --> 00:01:24,510
is this code on line 8 its really in the
wrong position that's should really be the

16
00:01:24,510 --> 00:01:32,850
last line of codes so I'm going to actually
move that down delete that line and

17
00:01:32,850 --> 00:01:36,230
then put it down right below all the
other definitions I'm going to put it

18
00:01:36,230 --> 00:01:43,600
down here and will be actually dealing
with talking about this a little bit

19
00:01:43,600 --> 00:01:48,850
later we're going to talk about an idea of a main function to put the code

20
00:01:48,850 --> 00:01:52,910
that's not directly in functions but we will
be doing that a little bit later and lastly

21
00:01:52,910 --> 00:02:00,920
on line 35 you may recall that I
talked in the previous video about making sure

22
00:02:00,920 --> 00:02:06,060
when we're actually grabbing cards the
that we don't pop the cards of the end of the list

23
00:02:06,060 --> 00:02:12,730
and that's what I did so next_car = deck.pop on line 35 is doing just that

24
00:02:12,730 --> 00:02:15,560
so we don't want to grab a card from the
bottom of the deck

25
00:02:15,560 --> 00:02:20,350
we want to grab a card from the top of
the deck obviously so that's a mistake on

26
00:02:20,350 --> 00:02:26,070
my behalf that should actually be a 0 their 0 meabs to pop it basically to grab the

27
00:02:26,070 --> 00:02:30,700
card that's sitting from the top of the
deck that's obviously the card that we

28
00:02:30,700 --> 00:02:34,300
actually want to grab ok so now that we've
done that we've finish off that bit of

29
00:02:34,300 --> 00:02:38,500
housekeeping the next thing we really
need to do is calculate the players

30
00:02:38,500 --> 00:02:41,930
score and displayed in the frame that
is in that little bit at the top of the screen

31
00:02:41,930 --> 00:02:47,090
that I pointed before now I want to
demonstrate the other reason why IntelliJ

32
00:02:47,090 --> 00:02:51,690
gives warnings about local variables
shadowing those from the main program

33
00:02:51,690 --> 00:02:56,300
of course we talked about shadowing in
previous videos and so the first attempt

34
00:02:56,300 --> 00:02:59,900
at running this function isn't going to
be the final one so although I'll be

35
00:02:59,900 --> 00:03:04,600
replacing this first line of code the
technique used is sometimes necessary so

36
00:03:04,600 --> 00:03:09,560
it's really important to understand it
and the reason for these warnings so in

37
00:03:09,560 --> 00:03:13,680
terms of the players scored the
sensible place to calculate the total is

38
00:03:13,680 --> 00:03:17,790
just after each card has been dealt so what
I'm gonna do is modify the deal_player

39
00:03:17,790 --> 00:03:22,370
function this is one
starting on the on line 46

40
00:03:22,370 --> 00:03:27,330
so the face value of a card can be
obtained from the tuple returned by the

41
00:03:27,330 --> 00:03:31,710
deal_card function so that
can be used to update the players total

42
00:03:31,710 --> 00:03:37,800
now the code also has to deal with the
two values that an ace can represent so

43
00:03:37,800 --> 00:03:42,180
in blackjack that's not actually too
difficult luckily because no matter

44
00:03:42,180 --> 00:03:47,520
how many aces a player has only one of
them at most can have the value 11

45
00:03:47,520 --> 00:03:51,930
of course a player will be bust because the
maximum score is 21 so the technique

46
00:03:51,930 --> 00:03:57,310
used here is to give the first ace the value 11 and add any subsequent ones the value of 1 now if

47
00:03:57,310 --> 00:04:01,150
the player goes bust by holding at least
one ace ten is subtracted from the

48
00:04:01,150 --> 00:04:04,990
total and a check for being bust is
perform again so that way it will actually

49
00:04:04,990 --> 00:04:10,300
give the player the option to treat an ace
as one instead of 11 so in order to do

50
00:04:10,300 --> 00:04:14,120
all this we actually really need two
more variables one to store the players

51
00:04:14,120 --> 00:04:18,570
total and another two to tract whether
or not the player holds an ace that has

52
00:04:18,570 --> 00:04:22,950
the value 11 so let's go ahead and add
those so come down here first

53
00:04:24,180 --> 00:04:28,610
just after the player_score_label I'm

54
00:04:28,610 --> 00:04:38,670
gonna add a line of code they're and I'm going to put...

55
00:04:38,670 --> 00:04:44,160
and also gonna set another variable.....

56
00:04:44,160 --> 00:04:51,010
...now I'm not sure if I've used this term before but
variables that are defined in the main

57
00:04:51,010 --> 00:04:56,230
part of the program rather than in a
function are called global variables and

58
00:04:56,230 --> 00:05:00,390
variables that only exist inside a function
are called local variables and I know we

59
00:05:00,390 --> 00:05:04,670
talked about that before so in this case
where I've actually added these two

60
00:05:04,670 --> 00:05:10,530
global variables on line 72 and
73 by putting them here outside of the

61
00:05:10,530 --> 00:05:15,300
function we're making them global
variables so ok now that I've actually got those 2

62
00:05:15,300 --> 00:05:19,570
variables set up we can go back to our deal_player function and start

63
00:05:19,570 --> 00:05:27,350
using it so I'm gonna start typing some code in so deal_player I'm gonna start by putting....

64
00:05:27,350 --> 00:05:32,220
.....

65
00:05:32,220 --> 00:05:39,780
...we are actually obtaining the result from the

66
00:05:39,780 --> 00:05:46,890
deal card function and need to do a bit of
checking here so....

67
00:05:46,890 --> 00:05:59,800
....

68
00:05:59,800 --> 00:06:05,480
...so by default and ace has got a value 1
and what we're saying here is if an ace

69
00:06:05,480 --> 00:06:10,110
was drawn from the card from the deck and the player hasn't

70
00:06:10,110 --> 00:06:15,730
already got an ace in their hand then we are
going to assign 11 to this particular

71
00:06:15,730 --> 00:06:24,570
card so next we are gonna put...

72
00:06:24,570 --> 00:06:28,010
......

73
00:06:28,010 --> 00:06:31,790
...and we need to make a change here this deal card we need to also

74
00:06:31,790 --> 00:06:35,710
make sure that we can return the face
value of the cards so we need to specify

75
00:06:35,710 --> 00:06:43,230
position 0 element 0 so basically we
are determining if its the right value and then

76
00:06:43,230 --> 00:06:49,160
were actually assigning the score so player score += card_value and will do a test here will

77
00:06:49,160 --> 00:07:00,350
put..... because we are gonna treat the

78
00:07:00,350 --> 00:07:09,920
ace as one if the total score is over 21
so put....

79
00:07:09,920 --> 00:07:31,370
....

80
00:07:33,380 --> 00:07:45,960
......

81
00:07:47,640 --> 00:07:54,150
......

82
00:07:54,950 --> 00:08:03,090
...so the only information
we get we can get on the outcome of the

83
00:08:03,090 --> 00:08:07,110
game is if the player goes bust and that's
because the deal were not get any more

84
00:08:07,110 --> 00:08:12,010
cards until the players finished their go so
we can set the result to the dealer

85
00:08:12,010 --> 00:08:15,690
winning if the player goes bust but any
other outcomes will have to wait until a

86
00:08:15,690 --> 00:08:20,560
dealer is getting cards now before going any
further I'm going to run the program and you can

87
00:08:20,560 --> 00:08:27,330
see that sort of behaves but there is a
subtle bugs ok so just have a look at this running

88
00:08:27,330 --> 00:08:33,049
now so if I click on player notice that the
score is being updated but if I click it

89
00:08:33,049 --> 00:08:37,520
again and noticed it's only storing the last value

90
00:08:38,140 --> 00:08:41,299
so its not actually calculating the total and reasons

91
00:08:41,299 --> 00:08:46,450
for that its going to be probably be pretty obvious if
we actually closet this and have a look that's

92
00:08:46,450 --> 00:08:51,410
because on line 47 we used a local
variable and overrode

93
00:08:51,410 --> 00:08:57,330
the global variable and set it to a value of
0 so consequently the player_score is only going to ever

94
00:08:57,330 --> 00:09:02,400
be the total of the last card that was
actually drawn now just before we go

95
00:09:02,400 --> 00:09:07,980
ahead and actually removed that and fix that error if we
just have a look at the error shadow name

96
00:09:07,980 --> 00:09:13,540
players score from out of scope the shadowing of

97
00:09:13,540 --> 00:09:17,820
player score is one of them the other is
that why don't we get the same warning about

98
00:09:17,820 --> 00:09:22,660
player ace because both variables
are defined in the main program but

99
00:09:22,660 --> 00:09:26,150
IntelliJ is really only warning about
one of them in the deal player

100
00:09:26,150 --> 00:09:29,840
functions so why is that the case so lets
actually do a bit of a mini challenge for you

101
00:09:29,840 --> 00:09:34,460
is looking at this method is
there a difference in the way that

102
00:09:34,460 --> 00:09:39,660
player_score and player_ace
are used in this function and that might

103
00:09:39,660 --> 00:09:44,580
lead to why IntelliJ is only warning about
player_score so stop the video now

104
00:09:44,580 --> 00:09:49,010
and see if you can figure out the difference between
player_score and also player_ace

105
00:09:49,010 --> 00:09:54,590
and also how they are used in this process
and if you think you got it come back and we'll continue

106
00:09:54,590 --> 00:09:57,590
on

107
00:09:58,720 --> 00:10:03,140
ok so hopefully managed to figure it out
and the answer is that the value of

108
00:10:03,140 --> 00:10:09,500
player_ace is used in the comparisons
on line 49 and 53 its being used but the

109
00:10:09,500 --> 00:10:14,430
difference between that and the player_score variables is that the

110
00:10:14,430 --> 00:10:18,940
function doesn't attempt to assign a new
value to player_ace but player_score

111
00:10:18,940 --> 00:10:25,680
is assigning new values so its first
set on line 47 to 0 and then the card value is added

112
00:10:25,680 --> 00:10:31,430
to it also on line 51 and then of course
ten is subtracted from it if an ace is held on

113
00:10:31,430 --> 00:10:36,400
line 54 and its because of this difference
in the way that they used that Python

114
00:10:36,400 --> 00:10:40,930
treats the variables differently inside each function so IntelliJ is just

115
00:10:40,930 --> 00:10:45,540
reporting here it's Python is that actually
making the rules so we can confirm that

116
00:10:45,540 --> 00:10:52,290
there is a difference by using command
click so I click on player_score and if we click it to

117
00:10:52,290 --> 00:10:57,600
see player_ace goes back to our definition
on line 83 that's control-click if

118
00:10:57,600 --> 00:11:05,340
you're on PC or Linux machine and go
back to our function again so player_score

119
00:11:05,340 --> 00:11:09,870
on the other hand If I clicked that
somewhere other than the definition its

120
00:11:09,870 --> 00:11:15,470
staying in the method itself so try again their but player_ace if I click that that

121
00:11:15,470 --> 00:11:18,860
goes down to where it was define on line 83 so that's just a quick way to determine

122
00:11:18,860 --> 00:11:24,580
local or global variable putting that function back again so when you use the

123
00:11:24,580 --> 00:11:29,210
name of a global variable in a function
Python assumes that you want to use the

124
00:11:29,210 --> 00:11:33,620
global variable and will happily let you until you try to change its value

125
00:11:33,620 --> 00:11:37,830
that's the key point there so soon as
you assign a new value to a global

126
00:11:37,830 --> 00:11:42,810
variable within a function Python then
creates a local variable with the same

127
00:11:42,810 --> 00:11:47,680
name and your cpde no longer refers to
the global variable so once you

128
00:11:47,680 --> 00:11:51,990
understand behavior it actually does
make sense and it actually saves you

129
00:11:51,990 --> 00:11:57,300
from introducing hard to spot bugs but
it can be an unexpected behavior until you

130
00:11:57,300 --> 00:12:00,860
do understand that and we can see that
in action in our deal_player

131
00:12:00,860 --> 00:12:06,290
function their now so at the moment we've used the
variable name player_score

132
00:12:06,290 --> 00:12:08,390
and assigned new values to it

133
00:12:08,390 --> 00:12:13,030
as a result Python's created a new
local variable called player_score

134
00:12:13,030 --> 00:12:17,170
and IntelliJ's complaining
by giving us a warning that its shadows

135
00:12:17,170 --> 00:12:21,360
the global variable so it is important
to pay attention to those warnings as

136
00:12:21,360 --> 00:12:25,220
they are the only clue that we really not
in fact changing the global variable

137
00:12:25,220 --> 00:12:31,560
player_score also inside the
deal_player function we use

138
00:12:31,560 --> 00:12:36,740
the variable name player_ace but we haven't
assigned in new values to it so as a

139
00:12:36,740 --> 00:12:42,310
result Python let's us refer to the global
variable and consequently there's no

140
00:12:42,310 --> 00:12:46,310
warning bells from IntelliJ about
shadowing because we in fact not doing that

141
00:12:46,310 --> 00:12:50,610
because we are not saving values and essentially not creating a

142
00:12:50,610 --> 00:12:55,220
local variable so I'm gonna end this video here
now in the next video will continue our

143
00:12:55,220 --> 00:12:59,650
discussion about this and then move on
and get this deal_player

144
00:12:59,650 --> 00:13:02,110
function working correctly in calculating

145
00:13:02,110 --> 00:13:07,500
the score for the entire hand the players
got rather than just last card so I'll see you in the next

146
00:13:07,500 --> 00:13:07,730
video

