WEBVTT

0
00:00.560 --> 00:02.900
Now let's move on to Hint 9.

1
00:03.050 --> 00:07.580
So we want to call calculate_score() at some point in our code,

2
00:07.580 --> 00:14.630
and if the computer or the user has a blackjack, or if the user's score is over 21, then the game

3
00:14.630 --> 00:15.560
is going to end.

4
00:15.800 --> 00:21.440
This might be a good time to start tidying up our code a little bit, because we've got our function

5
00:21.440 --> 00:25.340
over here, the calculate_score() function,

6
00:25.340 --> 00:29.870
and remember that you can only call a function after it's been declared.

7
00:29.870 --> 00:37.380
So for example, if I wanted to call this function over here, then I actually can't. I can't say, calculate_score()

8
00:37.380 --> 00:40.980
because it hasn't yet been declared.

9
00:40.980 --> 00:47.700
So instead what I'm going to do is I'm going to take this function, and I'm going to move it to where

10
00:47.700 --> 00:49.230
we dealt our cards.

11
00:49.620 --> 00:55.920
So now that I've moved my function to above the line where I want to call it, it's now become valid

12
00:55.920 --> 00:56.610
code.

13
00:56.610 --> 01:02.220
And this is the place where I want to call it, because it's only after I've dealt the user and the

14
01:02.220 --> 01:06.510
computer some cards can I actually calculate their scores.

15
01:06.540 --> 01:10.800
Firstly, I want to calculate the score using the user's cards.

16
01:10.800 --> 01:18.840
So this list of cards from the user gets passed into this function and uses all of the logic to output

17
01:18.840 --> 01:20.490
some sort of score.

18
01:20.740 --> 01:25.450
And then I'm going to store that score in a variable, which I'll call user_score.

19
01:25.780 --> 01:29.080
And I'm going to do the same thing for the computer.

20
01:29.080 --> 01:32.710
So computer score = calculate_score()...

21
01:32.710 --> 01:40.780
and remember it's always helpful to add a little bit of a Docstring to tell ourselves and other programmers

22
01:40.780 --> 01:42.220
what this function does.

23
01:42.220 --> 01:43.510
Let's quickly summarize it.

24
01:43.510 --> 01:53.060
So this function is going to, """Take a list of cards and return the score calculated from the cards."""

25
01:54.440 --> 02:00.380
So now when I open that parentheses, I know that I probably have to pass in my list of cards in order

26
02:00.380 --> 02:01.880
to get back the score.

27
02:01.910 --> 02:05.180
And now we've got the user_score and the computer_score.

28
02:05.180 --> 02:08.720
So this score could equal 0 if they got a blackjack,

29
02:08.720 --> 02:14.450
or it could just simply be the value of the cards that they hold all added up together.

30
02:15.290 --> 02:22.700
Now that we've called calculate_score(), we also want to make sure that if the computer, or the user has

31
02:22.700 --> 02:29.000
a blackjack, or if the user scores over 21, then we have to end the game.

32
02:29.810 --> 02:31.700
Let's write our if statements.

33
02:31.700 --> 02:42.360
If the user_score is equal to 0, or the computer_score is equal to 0, or the user_score is greater

34
02:42.360 --> 02:47.610
than 21, then in this case we're going to tell the game to end.

35
02:47.610 --> 02:52.680
So we could create a new variable called is_game_over,

36
02:53.430 --> 02:56.460
and we start out with False of course,

37
02:56.460 --> 03:01.750
but then when this happens we're going to change that variable to True instead.

38
03:02.440 --> 03:03.880
Our is _game_over

39
03:03.880 --> 03:06.520
variable is just a simple boolean,

40
03:06.640 --> 03:08.440
It starts out as False,

41
03:08.440 --> 03:12.040
and when certain conditions are met, then we change it to True.

42
03:12.070 --> 03:17.740
Now at the moment our program is not finished, so it's not really obvious how we're going to be using

43
03:17.740 --> 03:24.100
this is_game_over variable, but we're setting ourselves up for the next steps. Now that we're tracking

44
03:24.100 --> 03:25.480
if the game should end,

45
03:25.510 --> 03:28.930
we can look at this value to determine what to do next.

46
03:28.930 --> 03:31.180
So that's Hint 9 completed.

47
03:31.180 --> 03:34.570
Let's test our code and see how everything works so far.

48
03:34.960 --> 03:41.200
I'm going to add some print statements here so that I can see what the user's cards are, and what the

49
03:41.200 --> 03:42.550
user's score is.

50
03:42.550 --> 03:49.000
Using an f-string I'm going to show, "Your cards," so the user's cards, and the "current score".

51
03:49.960 --> 03:54.350
And I'm also going to print the, "Computer's first card."

52
03:54.380 --> 03:57.800
Remember when I first explained the rules of Blackjack?

53
03:57.800 --> 04:05.600
The dealer will reveal their first card, so you get a little bit of a clue as to what kind of hand

54
04:05.600 --> 04:06.560
they might have.

55
04:07.250 --> 04:13.040
So in here, we're going to insert the computer's cards, and we're only going to pick out the first

56
04:13.040 --> 04:13.760
item.

57
04:13.760 --> 04:15.800
So the one at index 0,

58
04:15.800 --> 04:20.100
and we do that using a square bracket after the name of the list.

59
04:21.300 --> 04:24.000
Let me just change that typo there.

60
04:24.000 --> 04:29.460
And now we're ready to give this program a run to see how it works.

61
04:29.490 --> 04:38.730
Now we've got our first cards showing which is the user_cards, and it's a list of two cards, a 3

62
04:38.730 --> 04:39.750
and a 2.

63
04:39.750 --> 04:42.840
A 3 and 2 adds up, of course, to 5.

64
04:42.840 --> 04:46.320
So the current score for the user is 5.

65
04:46.500 --> 04:52.440
Next, it shows us the computer's first card, and that is a 4.

66
04:53.070 --> 05:00.510
Now we can be reasonably assured that our code is working, and it's a good idea to regularly test your

67
05:00.510 --> 05:04.890
code so that you don't wait until the end, when there are a lot of problems and you don't know which

68
05:04.890 --> 05:06.780
part of the code is responsible.