WEBVTT 1 00:00:01.920 --> 00:00:06.700 ok so moving on with our blackjack program now just before we continue their is a 2 00:00:06.700 --> 00:00:12.780 few items of housekeeping that I want to go through firstly when we actually run 3 00:00:12.780 --> 00:00:17.240 the app I've mentioned before that it wasn't sort of looking the best so if I have 4 00:00:17.240 --> 00:00:22.260 look and run it you can see that there's a lot of white space and obviously as we start 5 00:00:22.260 --> 00:00:25.260 using it it gets better and the cards get dealt 6 00:00:25.920 --> 00:00:30.180 and its starting to take up some space so probably the easiest thing to fix right 7 00:00:30.180 --> 00:00:33.239 now is just a change this backgrounds so what I'm going to do is change the background 8 00:00:33.239 --> 00:00:36.429 so its green so that's number one 9 00:00:36.429 --> 00:00:41.940 go ahead and do and where I would do that would be just under the two lines that 10 00:00:41.940 --> 00:00:48.459 set the window title and the geometry now here I'm gonna type.... 11 00:00:48.459 --> 00:01:03.019 ...and if we run that we've got a green window that's going to look 12 00:01:03.019 --> 00:01:07.740 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:07.740 --> 00:01:10.310 look a bit nicer and bearing in mind that we haven't 14 00:01:10.310 --> 00:01:16.969 got an text showing here but that will be becoming but the other issue that I want to fix 15 00:01:16.969 --> 00:01:21.090 is this code on line 8 its really in the wrong position that's should really be the 16 00:01:21.090 --> 00:01:29.439 last line of codes so I'm going to actually move that down delete that line and 17 00:01:29.439 --> 00:01:32.810 then put it down right below all the other definitions I'm going to put it 18 00:01:32.810 --> 00:01:40.189 down here and will be actually dealing with talking about this a little bit 19 00:01:40.189 --> 00:01:45.439 later we're going to talk about an idea of a main function to put the code 20 00:01:45.439 --> 00:01:49.499 that's not directly in functions but we will be doing that a little bit later and lastly 21 00:01:49.499 --> 00:01:57.509 on line 35 you may recall that I talked in the previous video about making sure 22 00:01:57.509 --> 00:02:02.649 when we're actually grabbing cards the that we don't pop the cards of the end of the list 23 00:02:02.649 --> 00:02:09.310 and that's what I did so next_car = deck.pop on line 35 is doing just that 24 00:02:09.310 --> 00:02:12.140 so we don't want to grab a card from the bottom of the deck 25 00:02:12.140 --> 00:02:16.930 we want to grab a card from the top of the deck obviously so that's a mistake on 26 00:02:16.930 --> 00:02:22.650 my behalf that should actually be a 0 their 0 meabs to pop it basically to grab the 27 00:02:22.650 --> 00:02:27.280 card that's sitting from the top of the deck that's obviously the card that we 28 00:02:27.280 --> 00:02:30.880 actually want to grab ok so now that we've done that we've finish off that bit of 29 00:02:30.880 --> 00:02:35.080 housekeeping the next thing we really need to do is calculate the players 30 00:02:35.080 --> 00:02:38.510 score and displayed in the frame that is in that little bit at the top of the screen 31 00:02:38.510 --> 00:02:43.670 that I pointed before now I want to demonstrate the other reason why IntelliJ 32 00:02:43.670 --> 00:02:48.270 gives warnings about local variables shadowing those from the main program 33 00:02:48.270 --> 00:02:52.880 of course we talked about shadowing in previous videos and so the first attempt 34 00:02:52.880 --> 00:02:56.480 at running this function isn't going to be the final one so although I'll be 35 00:02:56.480 --> 00:03:01.180 replacing this first line of code the technique used is sometimes necessary so 36 00:03:01.180 --> 00:03:06.140 it's really important to understand it and the reason for these warnings so in 37 00:03:06.140 --> 00:03:10.269 terms of the players scored the sensible place to calculate the total is 38 00:03:10.269 --> 00:03:14.370 just after each card has been dealt so what I'm gonna do is modify the deal_player 39 00:03:14.370 --> 00:03:18.950 function this is one starting on the on line 46 40 00:03:18.950 --> 00:03:23.910 so the face value of a card can be obtained from the tuple returned by the 41 00:03:23.910 --> 00:03:28.290 deal_card function so that can be used to update the players total 42 00:03:28.290 --> 00:03:34.380 now the code also has to deal with the two values that an ace can represent so 43 00:03:34.380 --> 00:03:38.760 in blackjack that's not actually too difficult luckily because no matter 44 00:03:38.760 --> 00:03:44.100 how many aces a player has only one of them at most can have the value 11 45 00:03:44.100 --> 00:03:48.510 of course a player will be bust because the maximum score is 21 so the technique 46 00:03:48.510 --> 00:03:53.890 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:53.890 --> 00:03:57.739 the player goes bust by holding at least one ace ten is subtracted from the 48 00:03:57.739 --> 00:04:01.570 total and a check for being bust is perform again so that way it will actually 49 00:04:01.570 --> 00:04:06.880 give the player the option to treat an ace as one instead of 11 so in order to do 50 00:04:06.880 --> 00:04:10.700 all this we actually really need two more variables one to store the players 51 00:04:10.700 --> 00:04:15.150 total and another two to tract whether or not the player holds an ace that has 52 00:04:15.150 --> 00:04:19.530 the value 11 so let's go ahead and add those so come down here first 53 00:04:20.769 --> 00:04:25.190 just after the player_score_label I'm 54 00:04:25.190 --> 00:04:35.259 gonna add a line of code they're and I'm going to put... 55 00:04:35.259 --> 00:04:40.740 and also gonna set another variable..... 56 00:04:40.740 --> 00:04:47.599 ...now I'm not sure if I've used this term before but variables that are defined in the main 57 00:04:47.599 --> 00:04:52.810 part of the program rather than in a function are called global variables and 58 00:04:52.810 --> 00:04:56.979 variables that only exist inside a function are called local variables and I know we 59 00:04:56.979 --> 00:05:01.250 talked about that before so in this case where I've actually added these two 60 00:05:01.250 --> 00:05:07.110 global variables on line 72 and 73 by putting them here outside of the 61 00:05:07.110 --> 00:05:11.880 function we're making them global variables so ok now that I've actually got those 2 62 00:05:11.880 --> 00:05:16.159 variables set up we can go back to our deal_player function and start 63 00:05:16.159 --> 00:05:23.930 using it so I'm gonna start typing some code in so deal_player I'm gonna start by putting.... 64 00:05:23.930 --> 00:05:28.800 ..... 65 00:05:28.800 --> 00:05:36.360 ...we are actually obtaining the result from the 66 00:05:36.360 --> 00:05:43.479 deal card function and need to do a bit of checking here so.... 67 00:05:43.479 --> 00:05:56.380 .... 68 00:05:56.380 --> 00:06:02.069 ...so by default and ace has got a value 1 and what we're saying here is if an ace 69 00:06:02.069 --> 00:06:06.699 was drawn from the card from the deck and the player hasn't 70 00:06:06.699 --> 00:06:12.319 already got an ace in their hand then we are going to assign 11 to this particular 71 00:06:12.319 --> 00:06:21.159 card so next we are gonna put... 72 00:06:21.159 --> 00:06:24.590 ...... 73 00:06:24.590 --> 00:06:28.370 ...and we need to make a change here this deal card we need to also 74 00:06:28.370 --> 00:06:32.290 make sure that we can return the face value of the cards so we need to specify 75 00:06:32.290 --> 00:06:39.810 position 0 element 0 so basically we are determining if its the right value and then 76 00:06:39.810 --> 00:06:45.740 were actually assigning the score so player score += card_value and will do a test here will 77 00:06:45.740 --> 00:06:56.930 put..... because we are gonna treat the 78 00:06:56.930 --> 00:07:06.500 ace as one if the total score is over 21 so put.... 79 00:07:06.500 --> 00:07:27.950 .... 80 00:07:29.960 --> 00:07:42.540 ...... 81 00:07:44.220 --> 00:07:50.730 ...... 82 00:07:51.530 --> 00:07:59.670 ...so the only information we get we can get on the outcome of the 83 00:07:59.670 --> 00:08:03.690 game is if the player goes bust and that's because the deal were not get any more 84 00:08:03.690 --> 00:08:08.590 cards until the players finished their go so we can set the result to the dealer 85 00:08:08.590 --> 00:08:12.270 winning if the player goes bust but any other outcomes will have to wait until a 86 00:08:12.270 --> 00:08:17.140 dealer is getting cards now before going any further I'm going to run the program and you can 87 00:08:17.140 --> 00:08:23.910 see that sort of behaves but there is a subtle bugs ok so just have a look at this running 88 00:08:23.910 --> 00:08:29.630 now so if I click on player notice that the score is being updated but if I click it 89 00:08:29.630 --> 00:08:34.100 again and noticed it's only storing the last value 90 00:08:34.720 --> 00:08:37.880 so its not actually calculating the total and reasons 91 00:08:37.880 --> 00:08:43.030 for that its going to be probably be pretty obvious if we actually closet this and have a look that's 92 00:08:43.030 --> 00:08:47.990 because on line 47 we used a local variable and overrode 93 00:08:47.990 --> 00:08:53.910 the global variable and set it to a value of 0 so consequently the player_score is only going to ever 94 00:08:53.910 --> 00:08:58.980 be the total of the last card that was actually drawn now just before we go 95 00:08:58.980 --> 00:09:04.560 ahead and actually removed that and fix that error if we just have a look at the error shadow name 96 00:09:04.560 --> 00:09:10.120 players score from out of scope the shadowing of 97 00:09:10.120 --> 00:09:14.400 player score is one of them the other is that why don't we get the same warning about 98 00:09:14.400 --> 00:09:19.240 player ace because both variables are defined in the main program but 99 00:09:19.240 --> 00:09:22.730 IntelliJ is really only warning about one of them in the deal player 100 00:09:22.730 --> 00:09:26.420 functions so why is that the case so lets actually do a bit of a mini challenge for you 101 00:09:26.420 --> 00:09:31.040 is looking at this method is there a difference in the way that 102 00:09:31.040 --> 00:09:36.240 player_score and player_ace are used in this function and that might 103 00:09:36.240 --> 00:09:41.160 lead to why IntelliJ is only warning about player_score so stop the video now 104 00:09:41.160 --> 00:09:45.590 and see if you can figure out the difference between player_score and also player_ace 105 00:09:45.590 --> 00:09:51.170 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:51.170 --> 00:09:54.170 on 107 00:09:55.300 --> 00:09:59.720 ok so hopefully managed to figure it out and the answer is that the value of 108 00:09:59.720 --> 00:10:06.080 player_ace is used in the comparisons on line 49 and 53 its being used but the 109 00:10:06.080 --> 00:10:11.010 difference between that and the player_score variables is that the 110 00:10:11.010 --> 00:10:15.529 function doesn't attempt to assign a new value to player_ace but player_score 111 00:10:15.529 --> 00:10:22.269 is assigning new values so its first set on line 47 to 0 and then the card value is added 112 00:10:22.269 --> 00:10:28.010 to it also on line 51 and then of course ten is subtracted from it if an ace is held on 113 00:10:28.010 --> 00:10:32.980 line 54 and its because of this difference in the way that they used that Python 114 00:10:32.980 --> 00:10:37.519 treats the variables differently inside each function so IntelliJ is just 115 00:10:37.519 --> 00:10:42.120 reporting here it's Python is that actually making the rules so we can confirm that 116 00:10:42.120 --> 00:10:48.870 there is a difference by using command click so I click on player_score and if we click it to 117 00:10:48.870 --> 00:10:54.180 see player_ace goes back to our definition on line 83 that's control-click if 118 00:10:54.180 --> 00:11:01.920 you're on PC or Linux machine and go back to our function again so player_score 119 00:11:01.920 --> 00:11:06.459 on the other hand If I clicked that somewhere other than the definition its 120 00:11:06.459 --> 00:11:12.050 staying in the method itself so try again their but player_ace if I click that that 121 00:11:12.050 --> 00:11:15.440 goes down to where it was define on line 83 so that's just a quick way to determine 122 00:11:15.440 --> 00:11:21.160 local or global variable putting that function back again so when you use the 123 00:11:21.160 --> 00:11:25.790 name of a global variable in a function Python assumes that you want to use the 124 00:11:25.790 --> 00:11:30.200 global variable and will happily let you until you try to change its value 125 00:11:30.200 --> 00:11:34.410 that's the key point there so soon as you assign a new value to a global 126 00:11:34.410 --> 00:11:39.390 variable within a function Python then creates a local variable with the same 127 00:11:39.390 --> 00:11:44.260 name and your cpde no longer refers to the global variable so once you 128 00:11:44.260 --> 00:11:48.579 understand behavior it actually does make sense and it actually saves you 129 00:11:48.579 --> 00:11:53.880 from introducing hard to spot bugs but it can be an unexpected behavior until you 130 00:11:53.880 --> 00:11:57.449 do understand that and we can see that in action in our deal_player 131 00:11:57.449 --> 00:12:02.870 function their now so at the moment we've used the variable name player_score 132 00:12:02.870 --> 00:12:04.970 and assigned new values to it 133 00:12:04.970 --> 00:12:09.610 as a result Python's created a new local variable called player_score 134 00:12:09.610 --> 00:12:13.759 and IntelliJ's complaining by giving us a warning that its shadows 135 00:12:13.759 --> 00:12:17.949 the global variable so it is important to pay attention to those warnings as 136 00:12:17.949 --> 00:12:21.800 they are the only clue that we really not in fact changing the global variable 137 00:12:21.800 --> 00:12:28.149 player_score also inside the deal_player function we use 138 00:12:28.149 --> 00:12:33.329 the variable name player_ace but we haven't assigned in new values to it so as a 139 00:12:33.329 --> 00:12:38.899 result Python let's us refer to the global variable and consequently there's no 140 00:12:38.899 --> 00:12:42.899 warning bells from IntelliJ about shadowing because we in fact not doing that 141 00:12:42.899 --> 00:12:47.199 because we are not saving values and essentially not creating a 142 00:12:47.199 --> 00:12:51.800 local variable so I'm gonna end this video here now in the next video will continue our 143 00:12:51.800 --> 00:12:56.230 discussion about this and then move on and get this deal_player 144 00:12:56.230 --> 00:12:58.699 function working correctly in calculating 145 00:12:58.699 --> 00:13:04.089 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:04.089 --> 00:13:04.319 video