WEBVTT

0
00:00.170 --> 00:03.230
Now let's move on to Hint Number 10.

1
00:03.410 --> 00:08.660
It says that if the game has not ended, ask the user if they want to draw another card.

2
00:08.690 --> 00:13.550
If yes, then use the deal_card() function to add another card to the user_cards list,

3
00:13.580 --> 00:22.250
if no, then the game has again ended. Over here in the if statement, we can add an else, because if

4
00:22.250 --> 00:28.700
the game hasn't ended, as in they haven't gone over 21 or nobody's got a blackjack, then we're going

5
00:28.700 --> 00:32.120
to ask the user if they want to get another card.

6
00:32.120 --> 00:38.930
I'm going to use an input, and I'm going to ask the user to type 'y' to get another card,

7
00:38.930 --> 00:41.570
otherwise type 'n' to pass.

8
00:41.570 --> 00:48.080
And I'm going to save this input inside a variable which I'll call user_should_deal.

9
00:49.160 --> 00:55.280
Now we can check to see if this variable is equal to 'y',

10
00:55.430 --> 01:02.450
well, in that case, we're going to add another card to the user's card list by doing the same thing

11
01:02.450 --> 01:09.890
that we did in the beginning, where we dealt two cards to each player by saying usercards.append(),

12
01:10.160 --> 01:12.380
and inside the parentheses,

13
01:12.380 --> 01:16.880
the thing that we want to append is the output from deal_card().

14
01:17.780 --> 01:21.520
So just a quick check to make sure you've got all the parentheses there.

15
01:21.520 --> 01:24.880
And then we're ready to address the else.

16
01:25.120 --> 01:29.590
If they didn't type 'y', then that means they don't want another card.

17
01:29.590 --> 01:32.620
Well then in this case, the game has again ended.

18
01:32.620 --> 01:36.220
So we can say is_game_over = True.

19
01:37.900 --> 01:39.820
Let's take a look at the next step.

20
01:39.820 --> 01:42.610
How can we get our game to repeat itself?

21
01:42.730 --> 01:48.960
So Hint Number 11 tells us that the score will need to be rechecked with every new card drawn, and

22
01:48.960 --> 01:53.430
the checks in Hint 9 need to be repeated until the game ends.

23
01:53.460 --> 01:59.850
Notice how we've got this, flag is_game_over, which is set to True whenever the game ends.

24
01:59.970 --> 02:07.290
It starts off being False, and what this means is we can actually create a while loop, which basically

25
02:07.290 --> 02:09.490
keeps on calculating the user_score,

26
02:09.490 --> 02:16.540
and the computer_score, makes all of these checks, which are in Hint 9, and then repeats itself

27
02:16.540 --> 02:18.610
until the game ends.

28
02:18.820 --> 02:20.470
Let's go ahead and do that.

29
02:20.740 --> 02:26.980
After all the initial setup, we're going to create a while loop, and the while loop is going to be

30
02:26.980 --> 02:30.100
active until the game is over.

31
02:30.100 --> 02:37.800
So we can say while not is_game_over, then go ahead and carry out all of these instructions.

32
02:37.800 --> 02:39.570
So calculate the user_score,

33
02:39.570 --> 02:41.430
calculate the computer_score,

34
02:41.430 --> 02:46.080
check that nobody's gone over 21 and nobody's gotten a blackjack,

35
02:46.080 --> 02:49.290
and if the user wants more cards, then they should,

36
02:49.290 --> 02:51.420
if they don't, then the game is over.

37
02:51.420 --> 02:55.110
So let's go ahead and run and test our code.

38
02:55.110 --> 03:02.450
You can see that the first two cards we get are 10 and 5, which makes up a score of 15.

39
03:02.450 --> 03:05.870
So 15 is kind of nowhere near 21,

40
03:05.870 --> 03:09.620
so let's go ahead and type 'y' to get another card.

41
03:09.620 --> 03:12.620
You can see that our while loop is active.

42
03:12.620 --> 03:16.460
And it goes back to getting another card.

43
03:17.120 --> 03:22.730
And we end up with 10, 5, and 10, which unfortunately means that we're over 21,

44
03:22.730 --> 03:24.270
and so we lose.

45
03:24.270 --> 03:30.090
But if I tried this and I typed 'n', then you can see the game ends immediately.

46
03:30.900 --> 03:32.850
That worked perfectly.

47
03:33.000 --> 03:36.360
So now let's go back and tackle the next part.

48
03:36.390 --> 03:41.100
Hint number 12 says once the user is done, it's time to let the computer play.

49
03:41.130 --> 03:46.410
The computer should keep drawing cards as long as it has a score less than 17.

50
03:46.890 --> 03:52.910
So the while loop is responsible for dealing with when the user wants to keep drawing cards,

51
03:52.910 --> 04:00.230
but once that's done, then we can tackle Hint Number 12, which is how does the computer actually play?

52
04:00.230 --> 04:02.300
What is its strategy?

53
04:02.330 --> 04:09.380
Well, we know that the computer has to keep drawing cards as long as it has a score of less than 17,

54
04:09.380 --> 04:15.440
but of course, 0 which represents a blackjack, is also less than 17,

55
04:15.440 --> 04:19.640
but we don't want it to draw a card if it has a blackjack.

56
04:20.030 --> 04:30.650
We can represent this logic using a while loop, while the computer_score is not equal to 0, and

57
04:30.650 --> 04:38.630
the computer_score is less than 17, then in this case we want to keep drawing cards.

58
04:38.810 --> 04:46.460
We're going to take the computer's cards, and we're going to use the append to add a card by dealing

59
04:46.460 --> 04:48.410
another card to the computer.

60
04:48.860 --> 04:54.470
And then we're going to recalculate the computer_score so that it updates,

61
04:54.470 --> 04:58.850
and this while loop is evaluated on the latest score.

62
04:58.940 --> 05:04.960
So the computer_score is going to equal calculate_score() using the computer_cards,

63
05:05.050 --> 05:12.340
and that means that the updated computer_score is equal to the return value from calculate_score().

64
05:12.370 --> 05:18.100
Now one thing you'll notice here is that we've got a warning underline for the computer_score,

65
05:18.100 --> 05:24.400
and when we hover underneath it it says the "Name 'computer_score' can be undefined."

66
05:24.400 --> 05:26.170
So what does it mean here?

67
05:26.200 --> 05:32.310
Well notice that we first define this variable computer_score inside this other while loop.

68
05:32.340 --> 05:36.480
Now we're hoping that hopefully this happens first,

69
05:36.480 --> 05:41.250
and by the time we reach Line 46, we already have a value for computer_score,

70
05:41.250 --> 05:44.550
and this will happen in pretty much all cases.

71
05:44.550 --> 05:51.840
But just in case there is a glitch, and there is no value for computer_score, or even in the case where

72
05:51.840 --> 05:57.520
this entire while loop is skipped, then this computer_score will be equal to undefined.

73
05:57.520 --> 05:59.230
It won't actually exist.

74
05:59.230 --> 06:06.400
That variable won't have been created for us to check whether or not if it equals to 0, or if it's

75
06:06.400 --> 06:08.440
greater or less than 17.

76
06:08.440 --> 06:10.600
So how can we fix this?

77
06:10.600 --> 06:16.840
Well, right up here where we've got our user_cards and computer_cards where we defined all of our empty

78
06:16.840 --> 06:22.290
variables, we should go ahead and define this computer_score right here.

79
06:22.320 --> 06:27.750
Now we can't set it to equal 0 by default, because that means blackjack in our game.

80
06:27.750 --> 06:34.410
So let's set it to a number that we, when we test our code, will know for sure there is an issue.

81
06:34.410 --> 06:36.930
Let's set it to a negative number.

82
06:36.930 --> 06:45.960
So notice how as soon as I've got that variable defined somewhere outside of the while loop, this warning

83
06:45.960 --> 06:53.610
now goes away and it will always have a value, even if it is a value that signifies there's something

84
06:53.610 --> 06:54.900
wrong with our code.

85
06:55.140 --> 06:57.780
And that will help us debug as well.

86
06:58.440 --> 07:03.720
Now you'll notice that we've actually got a similar situation with the user_score.

87
07:03.720 --> 07:10.680
The user_score is only defined inside this while loop, so it's created as a variable here and only

88
07:10.680 --> 07:12.330
assigned as a variable here.

89
07:12.330 --> 07:17.490
And in future if we want to use it, we also need to do the same thing.

90
07:17.490 --> 07:23.130
So let's just also create a user_score and set it to -1 as well.

91
07:24.060 --> 07:31.170
So now that we've completed Hint number 12, it's time to move on to the next video where we're going

92
07:31.170 --> 07:33.300
to go through the final hints.