1
00:00:00,280 --> 00:00:02,009
Right. So how did that go?

2
00:00:02,580 --> 00:00:06,150
I hope it went really well and you've managed to complete it all by yourself.

3
00:00:06,660 --> 00:00:11,310
If you got stuck, I really recommend just having a sandwich, have a cup of tea,

4
00:00:11,580 --> 00:00:13,500
take a break, and then come back to it.

5
00:00:13,740 --> 00:00:17,460
You'd be surprised what your brain can do when it just has a little bit of rest.

6
00:00:18,120 --> 00:00:21,840
But I want you to make sure that you've really given it a proper go before you

7
00:00:21,840 --> 00:00:23,700
come here and watch the solution.

8
00:00:24,120 --> 00:00:27,990
Cause I'm going to walk you through the solution step by step in case there are

9
00:00:28,200 --> 00:00:32,159
little bits or something that you didn't understand or something that you want

10
00:00:32,159 --> 00:00:33,960
to check. But I really,

11
00:00:33,960 --> 00:00:38,250
really hope that you've managed to tackle most of this yourself. All right.

12
00:00:38,250 --> 00:00:42,660
So without further ado, let's get started coding up our Blackjack project.

13
00:00:43,740 --> 00:00:47,130
Hopefully you've already used hints 1, 2 or 3,

14
00:00:47,550 --> 00:00:52,320
and I'm going to get started with hint 4 and build up our Blackjack project

15
00:00:52,350 --> 00:00:56,760
step-by-step. Now the idea of the hints 1, 2,

16
00:00:56,760 --> 00:01:01,760
and 3 is to get you to create these small to-dos of your own so that you

17
00:01:03,300 --> 00:01:08,220
can have practice breaking down the problem into a list and then breaking it

18
00:01:08,220 --> 00:01:13,170
down into a flowchart and then breaking it down into smaller bite-size to-dos.

19
00:01:13,860 --> 00:01:15,750
So let's start out with the first one,

20
00:01:16,350 --> 00:01:21,350
we have to create a deal_card function that uses the list below to return a

21
00:01:22,470 --> 00:01:27,180
random card and 11 is going to be the Ace in this deck.

22
00:01:27,990 --> 00:01:32,850
So let's get started by forking a copy of this repl and we're going to go and

23
00:01:32,850 --> 00:01:34,740
create our deal_card function.

24
00:01:35,610 --> 00:01:39,810
Now this function is going to contain these cards,

25
00:01:39,840 --> 00:01:43,770
so I'm going to go ahead and paste it in, make sure it's indented

26
00:01:43,830 --> 00:01:48,830
so it's inside the function. And now I'm going to use these cards to randomly

27
00:01:49,650 --> 00:01:52,650
pick one out of this list. So to do that,

28
00:01:52,680 --> 00:01:57,210
we can of course import our random module.

29
00:01:57,840 --> 00:02:02,840
And then I'm going to use random.choice to pick a random card from my list

30
00:02:03,900 --> 00:02:07,740
of cards. And then this of course has an output,

31
00:02:07,770 --> 00:02:09,030
this random.choice

32
00:02:09,330 --> 00:02:14,330
so I'm gonna save that output, namely the random card, inside a variable called

33
00:02:15,480 --> 00:02:16,313
card.

34
00:02:16,770 --> 00:02:21,540
And then finally we're going to return this chosen card as the output.

35
00:02:22,200 --> 00:02:23,910
So that's step 4 done.

36
00:02:25,590 --> 00:02:27,480
Now let's move on to step 5.

37
00:02:28,110 --> 00:02:33,030
So we're going to deal the user and computer two cards each using the deal card

38
00:02:33,030 --> 00:02:35,310
function, and very helpfully

39
00:02:35,340 --> 00:02:38,760
the hint tells us that we can start out with a blank

40
00:02:39,030 --> 00:02:42,810
user_card and a blank computer_card list.

41
00:02:43,020 --> 00:02:46,110
So we basically just have to add two cards to

42
00:02:46,110 --> 00:02:48,660
each of these lists. To do this,

43
00:02:48,660 --> 00:02:53,660
I'm going to use a for loop and I'm going to use the range operator to run this

44
00:02:55,560 --> 00:02:56,790
for-loop twice.

45
00:02:57,150 --> 00:03:01,480
So you've seen this syntax before when we talked about for loops and I've got an

46
00:03:01,510 --> 00:03:05,620
underscore here because we don't actually need this particular variable.

47
00:03:05,920 --> 00:03:08,650
All we need is for this loop to run twice

48
00:03:08,770 --> 00:03:13,390
and this code is going to achieve that. Every single time this loop runs

49
00:03:13,420 --> 00:03:16,840
we're going to get a new card by calling deal_card.

50
00:03:17,530 --> 00:03:20,440
Now remember that deal_card has an output,

51
00:03:20,740 --> 00:03:23,800
it outputs a random card. So if you want,

52
00:03:23,800 --> 00:03:27,160
it's actually quite helpful to add some documentation to this.

53
00:03:27,400 --> 00:03:32,400
So we could say returns a random card from the deck.

54
00:03:34,720 --> 00:03:37,510
And now when I write deal_card

55
00:03:37,570 --> 00:03:41,560
you can see that the doc tells me that this function is going to return a random

56
00:03:41,560 --> 00:03:46,330
card from the deck. So that random card is going to replace this function call

57
00:03:46,420 --> 00:03:50,320
so we can capture that data by storing it inside of a variable.

58
00:03:50,740 --> 00:03:55,740
So we'll call that new_card and set it equal to the output from the function

59
00:03:57,670 --> 00:03:59,680
deal_card. Now,

60
00:03:59,770 --> 00:04:04,690
the next thing we're going to do is we're going to add this new_card to the

61
00:04:04,690 --> 00:04:05,860
user's cards.

62
00:04:06,100 --> 00:04:11,100
So at the moment, it's an empty list and all we have to do is say user_cards

63
00:04:11,890 --> 00:04:13,060
.append

64
00:04:13,510 --> 00:04:17,890
and then the object that we want to add to that list is going to be the new_

65
00:04:17,890 --> 00:04:18,723
card.

66
00:04:19,240 --> 00:04:24,240
Now some of you might be tempted to instead use the plus equals.

67
00:04:25,360 --> 00:04:30,280
You might want to write something like this; user_cards += new_card.

68
00:04:30,970 --> 00:04:34,030
And I want to show you what actually happens when yourun this.

69
00:04:34,030 --> 00:04:35,770
cause this is a really common error.

70
00:04:36,370 --> 00:04:39,160
We get a trace back and we get a type error.

71
00:04:39,370 --> 00:04:44,370
It tells us that the int object is not iterable and it highlights this line 49

72
00:04:46,390 --> 00:04:50,500
as the reason for the error. So what's going on here?

73
00:04:51,130 --> 00:04:51,400
Well,

74
00:04:51,400 --> 00:04:56,400
this += is actually shorthand for writing the extend function.

75
00:04:58,180 --> 00:05:02,410
Now you can see that the extend function is something that extends a list

76
00:05:02,770 --> 00:05:07,360
by appending elements from the iterable. So what does this mean? Well,

77
00:05:07,360 --> 00:05:11,410
it means that whatever you put inside this parentheses,

78
00:05:11,770 --> 00:05:15,340
it has to be a list itself. For example,

79
00:05:15,850 --> 00:05:20,680
if this new_card was a list of cards,

80
00:05:20,830 --> 00:05:24,970
so maybe just the first dealt card and another card

81
00:05:25,300 --> 00:05:29,500
or even just it by itself inside a set of square brackets

82
00:05:29,590 --> 00:05:34,240
which turns it into a list, well then if we run this code right now

83
00:05:35,680 --> 00:05:40,420
we no longer have any errors and if I convert this back to what you saw before,

84
00:05:40,450 --> 00:05:44,260
which is using the plus equals, we also have no errors.

85
00:05:44,920 --> 00:05:49,780
But when you only want to add a single item, not a list, to an existing list

86
00:05:50,020 --> 00:05:53,770
then you have to use append. So hopefully you didn't have this issue,

87
00:05:54,100 --> 00:05:54,940
but if you did,

88
00:05:55,000 --> 00:05:59,750
this is the reason. Now that we've created these two lines,

89
00:05:59,810 --> 00:06:04,340
you can see that it's a little bit redundant because we're creating this new

90
00:06:04,340 --> 00:06:08,120
card variable just to hold the output from this function.

91
00:06:08,600 --> 00:06:12,710
And then we're immediately putting it into this list using the append.

92
00:06:13,220 --> 00:06:18,220
So instead we can actually just get rid of the middleman and put deal_card

93
00:06:18,980 --> 00:06:21,440
directly inside the append.

94
00:06:22,010 --> 00:06:25,460
And this does exactly the same thing in a single line of code.

95
00:06:26,360 --> 00:06:28,430
Once we've done this for user_cards,

96
00:06:28,460 --> 00:06:31,610
we'd probably want to do the same thing for the computers cards.

97
00:06:32,000 --> 00:06:36,620
So it computer_cards.append, and then we call deal_card again,

98
00:06:36,680 --> 00:06:37,970
remembering the parentheses.

99
00:06:38,210 --> 00:06:43,210
So we've got a total of three parentheses at the end and this way we deal a new

100
00:06:43,520 --> 00:06:47,360
card and that new card gets appended to the end of this 

101
00:06:47,920 --> 00:06:48,010
list.

