WEBVTT

0
00:00.230 --> 00:00.620
All right.

1
00:00.620 --> 00:02.300
So how did that go?

2
00:02.330 --> 00:03.770
I hope it went really well,

3
00:03.770 --> 00:07.700
and you've managed to complete it all by yourself. If you got stuck,

4
00:07.700 --> 00:10.430
I really recommend just having a sandwich,

5
00:10.430 --> 00:13.640
have a cup of tea, take a break, and then come back to it.

6
00:13.640 --> 00:17.780
You'd be surprised what your brain can do when it just has a little bit of rest.

7
00:17.780 --> 00:23.030
But I want you to make sure that you've really given it a proper go before you come here and watch the

8
00:23.030 --> 00:28.430
solution, because I'm going to walk you through the solution step by step in case there are little

9
00:28.460 --> 00:32.830
bits or something that you didn't understand, or something that you want to check.

10
00:32.830 --> 00:37.300
But I really, really hope that you've managed to tackle most of this yourself.

11
00:37.660 --> 00:42.730
All right, so without further ado, let's get started coding up our Blackjack Project.

12
00:43.570 --> 00:47.260
Hopefully you've already used, Hints 1, 2 or 3.

13
00:47.260 --> 00:53.380
And I'm going to get started with Hint 4 and build up our Blackjack project step by step.

14
00:53.950 --> 01:02.710
Now the idea of the Hint 1, 2, and 3 is to get you to create these small TODOs of your own

15
01:02.710 --> 01:08.770
so that you can have practice breaking down the problem into a list, and then breaking it down into

16
01:08.770 --> 01:13.450
a flowchart, and then breaking it down into smaller, bite-sized TODOs.

17
01:13.540 --> 01:16.000
So let's start out with the first one.

18
01:16.060 --> 01:25.320
We have to create a deal_card() function that uses the list below to return a random card, and 11 is

19
01:25.320 --> 01:27.270
going to be the Ace in this deck.

20
01:27.870 --> 01:29.100
So let's get started.

21
01:29.100 --> 01:32.430
And we're going to go and create our deal_card() function.

22
01:32.640 --> 01:37.110
Now this function is going to contain these cards,

23
01:37.110 --> 01:39.660
so I'm going to go ahead and paste it in,

24
01:39.660 --> 01:41.070
make sure it's indented.

25
01:41.070 --> 01:43.020
So it's inside the function.

26
01:43.110 --> 01:48.780
And now I'm going to use these cards to randomly pick one out of this list.

27
01:48.780 --> 01:54.780
So to do that we can of course import our Random Module.

28
01:54.780 --> 02:02.130
And then I'm going to use random.choice() to pick a random card from my list of cards.

29
02:02.130 --> 02:06.480
And then this of course has an output, this random.choice().

30
02:06.480 --> 02:13.680
So I'm going to save that output, namely the random card inside another variable called card.

31
02:13.770 --> 02:19.170
And then finally we're going to return this chosen card as the output.

32
02:19.200 --> 02:21.510
So that's Step 4 done.

33
02:22.650 --> 02:25.110
Now let's move on to Step 5.

34
02:25.200 --> 02:31.170
So we're going to deal the user and computer two cards each using the deal_card() function.

35
02:31.170 --> 02:39.440
And very helpfully, the hint tells us that we can start out with a blank user card and a blank computer

36
02:39.440 --> 02:40.220
card list.

37
02:40.220 --> 02:44.840
So we basically just have to add two cards to each of these lists.

38
02:44.960 --> 02:53.450
To do this, I'm going to use a for loop, and I'm going to use the range() operator to run this for loop

39
02:53.450 --> 02:54.230
twice.

40
02:54.230 --> 02:58.010
So you've seen this syntax before when we talked about for loops.

41
02:58.010 --> 03:03.160
And I've got an underscore here because we don't actually need this particular variable,

42
03:03.160 --> 03:05.980
all we need is for this loop to run twice,

43
03:05.980 --> 03:10.720
and this code is going to achieve that. Every single time this loop runs,

44
03:10.720 --> 03:14.440
we're going to get a new card by calling deal_card().

45
03:14.560 --> 03:17.860
Now remember that deal_card() has an output.

46
03:17.860 --> 03:20.140
It outputs a random card.

47
03:20.140 --> 03:24.520
So if you want it's actually quite helpful to add some documentation to this.

48
03:24.520 --> 03:30.270
So we could say, """Returns a random card from the deck."""

49
03:31.680 --> 03:38.490
And now when I write deal_card(), you can see that the doc tells me that this function is going to return

50
03:38.490 --> 03:40.110
a random card from the deck.

51
03:40.110 --> 03:43.650
So that random card is going to replace this function call.

52
03:43.650 --> 03:47.820
So we can capture that data by storing it inside a variable.

53
03:47.820 --> 03:56.070
So we'll call that new_card, and set it equal to the output from the function deal_card().

54
03:56.370 --> 04:03.210
Now the next thing we're going to do is we're going to add this new_card to the user_cards.

55
04:03.210 --> 04:05.250
So at the moment it's an empty list,

56
04:05.250 --> 04:10.560
and all we have to do is say user_cards.append(),

57
04:10.560 --> 04:16.040
and then the object that we want to add to that list is going to be the new_card.

58
04:16.280 --> 04:22.220
Now, some of you might be tempted to instead use the +=,

59
04:22.430 --> 04:24.620
you might want to write something like this.

60
04:24.620 --> 04:27.920
User cards += new_card.

61
04:28.010 --> 04:31.340
And I want to show you what actually happens when you run this.

62
04:31.340 --> 04:33.410
Because this is a really common error.

63
04:33.410 --> 04:36.560
We get a Traceback, and we get a TypeError.

64
04:36.560 --> 04:45.260
It tells us that the 'int' object is not iterable and it highlights this line 49 as the reason for the

65
04:45.260 --> 04:45.740
error.

66
04:46.370 --> 04:48.140
So what's going on here?

67
04:48.170 --> 04:55.250
Well, this plus equals is actually shorthand for writing the extend() function.

68
04:55.250 --> 05:02.030
Now you can see that the extend function is something that extends a list by appending elements from

69
05:02.030 --> 05:03.050
the iterable.

70
05:03.050 --> 05:04.460
So what does this mean?

71
05:04.460 --> 05:11.500
Well, it means that whatever you put inside these parentheses, it has to be a list itself.

72
05:11.530 --> 05:21.760
For example, if this new_card was a list of cards, so maybe just the first dealt card and another

73
05:21.760 --> 05:28.750
card, or even just it by itself inside a set of square brackets which turns it into a list,

74
05:28.750 --> 05:34.810
well then if we run this code right now, we no longer have any errors.

75
05:34.810 --> 05:41.110
And if I convert this back to what you saw before, which is using the p+=, we also have no

76
05:41.110 --> 05:41.920
errors.

77
05:41.920 --> 05:48.190
But when you only want to add a single item, not a list to an existing list, then you have to use

78
05:48.190 --> 05:48.850
append().

79
05:48.850 --> 05:53.620
So hopefully you didn't have this issue, but if you did, this is the reason.

80
05:54.190 --> 06:00.360
Now that we've created these two lines, you can see that it's a little bit redundant because we're

81
06:00.360 --> 06:05.670
creating this new_card variable just to hold the output from this function,

82
06:05.670 --> 06:10.320
and then we're immediately putting it into this list using the append().

83
06:10.320 --> 06:19.020
So instead we can actually just get rid of the middleman and put deal_card() directly inside the append.

84
06:19.020 --> 06:23.160
And this does exactly the same thing in a single line of code.

85
06:23.430 --> 06:29.100
Once we've done this for user_cards, we'd probably want to do the same thing for the computer_cards.

86
06:29.100 --> 06:35.370
So computer_cards.append(), and then we call deal_card() again remembering the parentheses.

87
06:35.370 --> 06:38.310
So we've got a total of three parentheses at the end.

88
06:38.310 --> 06:41.490
And this way we deal a new_card

89
06:41.490 --> 06:45.450
and that new_card gets appended to the end of this list.