1
00:00:00,810 --> 00:00:04,620
Now let's move on to a hint 9. So we want to call

2
00:00:04,620 --> 00:00:07,350
calculate_score at some point in our code

3
00:00:07,920 --> 00:00:10,830
and if the computer or the user has a Blackjack 

4
00:00:11,250 --> 00:00:15,390
or if the user's score is over 21, then the game is going to end.

5
00:00:15,900 --> 00:00:19,440
This might be a good time to start tidying up our code a little bit,

6
00:00:19,860 --> 00:00:22,320
because we've got our function over here,

7
00:00:23,310 --> 00:00:25,140
the calculate score function.

8
00:00:25,710 --> 00:00:29,790
And remember that you can only call a function after it's been declared.

9
00:00:30,150 --> 00:00:34,500
So for example, if I wanted to call this function over here,

10
00:00:34,860 --> 00:00:36,090
then I actually can't.

11
00:00:36,090 --> 00:00:40,710
I can't say calculate_score because it hasn't yet been declared.

12
00:00:41,310 --> 00:00:42,210
So instead,

13
00:00:42,240 --> 00:00:46,980
what I'm going to do is I'm going to take this function and I'm going to move it

14
00:00:47,220 --> 00:00:51,720
to where we dealt our cards and I'm going to collapse some of these functions so

15
00:00:51,720 --> 00:00:55,080
I can see my hints and my code a little bit easier.

16
00:00:56,040 --> 00:01:00,360
So now that I've moved my function to above the line where I want to call it,

17
00:01:00,690 --> 00:01:02,430
it's now become valid code.

18
00:01:02,970 --> 00:01:07,530
And this is the place where I want to call it because it's only after I've dealt

19
00:01:07,530 --> 00:01:12,390
the user and the computer some cards can I actually calculate their scores.

20
00:01:12,900 --> 00:01:16,740
Firstly, I want to calculate the score using the user's cards.

21
00:01:17,160 --> 00:01:22,160
So this list of cards from the user gets passed into this function and uses

22
00:01:22,800 --> 00:01:26,280
all of the logic to output some sort of score.

23
00:01:27,180 --> 00:01:29,820
And then I'm going to store that score in a variable

24
00:01:29,820 --> 00:01:31,290
which I'll call user_score.

25
00:01:32,190 --> 00:01:35,160
And I'm going to do the same thing for the computer,

26
00:01:35,190 --> 00:01:40,170
so computer_score equals calculate_score. And remember,

27
00:01:40,440 --> 00:01:45,440
it's always helpful to add a little bit of a docstring to tell ourselves and

28
00:01:45,870 --> 00:01:49,650
other programmers what this function does. Let's quickly summarize it.

29
00:01:49,650 --> 00:01:54,650
So this function is going to take a list of cards and return the score

30
00:01:57,120 --> 00:01:58,980
calculated from the cards.

31
00:02:00,780 --> 00:02:02,790
So now when I open that parentheses,

32
00:02:03,150 --> 00:02:07,200
I know that I probably have to pass in my list of cards in order to get back the

33
00:02:07,200 --> 00:02:08,032
score.

34
00:02:08,130 --> 00:02:12,480
So let's put in the computer_cards in as the inputs,

35
00:02:12,900 --> 00:02:15,690
and now we've got the user_score and the computer_score.

36
00:02:16,110 --> 00:02:18,000
So this score could equal zero

37
00:02:18,030 --> 00:02:22,890
if they got a Blackjack or it could just simply be the value of the cards that

38
00:02:22,890 --> 00:02:24,930
they hold all added up together.

39
00:02:26,070 --> 00:02:28,620
Now that we've called calculate_score

40
00:02:29,040 --> 00:02:34,040
we also want to make sure that if the computer or the user has a Blackjack 

41
00:02:34,710 --> 00:02:39,450
or if the user's score is over 21, then we have to end the game.

42
00:02:40,740 --> 00:02:45,510
Let's write our if statements. If the user_score is equal to zero,

43
00:02:46,080 --> 00:02:50,130
or the computer_score is equal to zero,

44
00:02:50,760 --> 00:02:55,740
or the user_score is greater than 21, then in this case

45
00:02:55,770 --> 00:02:58,140
we're going to tell the game to end.

46
00:02:58,560 --> 00:03:03,130
So we could create a new variable called is_game_over

47
00:03:04,450 --> 00:03:08,890
and we start out with false, of course. But then when this happens,

48
00:03:08,950 --> 00:03:12,160
we're going to change that variable to true instead.

49
00:03:13,180 --> 00:03:16,960
Our is_game_over variable is just a simple boolean.

50
00:03:17,620 --> 00:03:21,100
It starts out as false and when certain conditions are met,

51
00:03:21,310 --> 00:03:25,660
then we change it to true. Now at the moment, our program is not finished.

52
00:03:25,750 --> 00:03:29,880
So it was not really obvious how we're going to be using this is_game_over

53
00:03:29,890 --> 00:03:33,220
variable. But we're setting ourselves up for the next steps.

54
00:03:33,670 --> 00:03:36,190
Now that we're tracking if the game should end,

55
00:03:36,400 --> 00:03:39,520
we can look at this value to determine what to do next.

56
00:03:39,880 --> 00:03:44,740
So that's hint 9 completed. Let's test our code and see how everything works so

57
00:03:44,740 --> 00:03:45,573
far.

58
00:03:45,820 --> 00:03:49,840
I'm going to add some print statements here so that I can see what the user's

59
00:03:49,840 --> 00:03:54,670
cards are and what the user's score is. Using an fstring

60
00:03:54,730 --> 00:03:59,470
I'm going to show your cards, so the user's cards, and the current score.

61
00:04:00,880 --> 00:04:04,810
And I'm also going to print the computers first card.

62
00:04:05,320 --> 00:04:08,350
Remember when I first explained the rules of Blackjack,

63
00:04:08,710 --> 00:04:12,220
the dealer will reveal their first card

64
00:04:12,640 --> 00:04:17,050
so you get a little bit of a clue as to what kind of hand they might have.

65
00:04:18,160 --> 00:04:21,310
So in here, we're going to insert the computer's cards

66
00:04:21,760 --> 00:04:26,470
and we're only gonna pick out the first item, so that the one at index zero,

67
00:04:26,590 --> 00:04:30,610
and we do that using a square bracket after the name of the list.

68
00:04:31,960 --> 00:04:36,220
Now let's give our program a run and see if it actually works.

69
00:04:36,820 --> 00:04:40,780
The first thing that prints is a list of our cards.

70
00:04:40,840 --> 00:04:41,920
So in our hand,

71
00:04:41,950 --> 00:04:46,180
we have a 2 and we have a 6 and 2 + 6 added together

72
00:04:46,210 --> 00:04:48,040
of course makes the score of 8.

73
00:04:48,670 --> 00:04:52,630
And then it reveals to us the computer's first card, which is a 10.

74
00:04:53,320 --> 00:04:57,730
Now we can be reasonably assured that our code is working.

75
00:04:58,330 --> 00:05:02,170
And it's a good idea to regularly test your code so that you don't wait until

76
00:05:02,170 --> 00:05:04,120
the end when they're a lot of problems

77
00:05:04,360 --> 00:05:06,790
and you don't know which part of the code is responsible.

