WEBVTT

0
00:00.110 --> 00:00.410
All right.

1
00:00.410 --> 00:03.290
So now let's tackle Hint Number 6.

2
00:03.320 --> 00:06.800
We're going to create a function called calculate_score(),

3
00:06.800 --> 00:09.710
that's going to take a list of cards as an input,

4
00:09.710 --> 00:13.370
and then it returns the score after calculation.

5
00:13.460 --> 00:18.650
And it tells us that if we get stuck we can look up the sum() function to help us.

6
00:18.770 --> 00:22.760
So let's create a function called calculate_score().

7
00:22.760 --> 00:27.950
And this calculate_score() is going to take some cards as an input,

8
00:27.950 --> 00:34.920
and then inside the body, it's going to calculate the total of all the cards in this list.

9
00:34.950 --> 00:43.110
So if we take a look at the sum() function in Python, the way that it works is you can put an iterable,

10
00:43.110 --> 00:48.120
so like a list, inside the parentheses as an input,

11
00:48.120 --> 00:52.380
and then it's going to add up all of the items in the list,

12
00:52.380 --> 00:54.630
and it's going to return the total.

13
00:55.280 --> 00:55.670
Back

14
00:55.670 --> 01:03.500
in our code, we can simply write sum(), and then inside these parentheses, we can pass over the cards and

15
01:03.500 --> 01:07.010
we can return this value as the output.

16
01:07.550 --> 01:17.960
Basically, if we had cards equal to a list with [1, 5, 3, 4], then this sum() function is going to end up being

17
01:17.960 --> 01:23.160
equal to 1 + 5 + 3 + 4, which is going to be equal to 13.

18
01:23.160 --> 01:26.640
And then that will become the output of this function.

19
01:27.240 --> 01:29.910
So now let's move on to Hint 7.

20
01:30.660 --> 01:35.670
A Blackjack only happens when we have a hand with only 2 cards,

21
01:35.670 --> 01:40.770
and those cards have to be an Ace and a 10-value card.

22
01:41.430 --> 01:50.050
In our deck, we know that an Ace is represented by 11 and we have a number of 10-value cards.

23
01:50.230 --> 01:54.610
So there's a number of ways that we can check for this Blackjack.

24
01:54.640 --> 02:01.660
One of the ways is by checking to see if the 11 is in our deck of cards,

25
02:01.780 --> 02:08.650
and then we can combine that with an and, to check if the 10 is also in our deck of cards.

26
02:08.650 --> 02:15.930
And finally, we can check to make sure that we've got a hand size of 2. So we can check that the length

27
02:15.930 --> 02:19.410
of our cards is equal to 2,

28
02:19.440 --> 02:22.170
and this would represent this logic.

29
02:22.200 --> 02:24.330
A hand with only 2 cards,

30
02:24.330 --> 02:28.350
an Ace which is represented by 11 and a 10.

31
02:28.560 --> 02:35.600
Now, on the other hand, we could actually also simplify this, because a hand with only 2 cards that

32
02:35.600 --> 02:38.600
must contain an 11 and a 10, we can

33
02:38.600 --> 02:44.390
instead of checking for both 11 and 10, we can check to see if the total...

34
02:44.390 --> 02:53.210
So if we summed the cards, and if this was equal to 21, then it would be exactly the same thing, right?

35
02:53.240 --> 02:55.160
10 + 11 is 21,

36
02:55.160 --> 02:58.280
and we've still only got a deck of 2.

37
02:58.640 --> 02:59.750
In this case,

38
02:59.750 --> 03:04.790
what we want to happen is we want to return 0 instead of the actual score.

39
03:04.790 --> 03:11.630
So this way we can indicate that the user or the computer has got a score of Blackjack.

40
03:12.470 --> 03:15.020
That's Hint Number 7 sorted,

41
03:15.020 --> 03:17.210
let's move on to Hint Number 8.

42
03:17.390 --> 03:24.950
It tells us that inside calculate_score(), we should check also for an 11, which is an Ace. If the score

43
03:24.970 --> 03:32.590
is already over 21, then we should remove the 11 from the cards and we could replace it with a 1,

44
03:32.590 --> 03:37.120
because remember that an Ace can count as an 11 or a 1.

45
03:37.120 --> 03:43.300
So in the beginning, when the user's score is very small, then we probably want to count it as an

46
03:43.300 --> 03:43.930
11,

47
03:43.930 --> 03:50.420
but once the user goes over 21, then we probably want to change it and count it as a 1 instead,

48
03:50.420 --> 03:52.790
so they don't go over and they don't lose.

49
03:53.810 --> 03:55.940
Let's first write the if statement.

50
03:55.940 --> 04:07.460
So if there is the 11 inside our deck of cards and the sum of the cards, so the total score, is already

51
04:07.460 --> 04:11.360
over 21, then we want to do something about it.

52
04:11.360 --> 04:15.750
And the hint tells us to look up the append() and remove() functions.

53
04:15.750 --> 04:22.200
So we've already seen the append() function many times, and we know that it just adds a single element

54
04:22.200 --> 04:23.730
to the end of the list.

55
04:24.360 --> 04:31.200
Now, the one that we haven't used a lot of, is the remove() function, and this will search for the first

56
04:31.200 --> 04:35.910
instance of the given element and removes it out of the list.

57
04:36.300 --> 04:38.370
This is kind of what we need to do.

58
04:38.370 --> 04:45.890
We need to go ahead and remove the 11 from the list of cards, so we can write cards.remove(), and

59
04:45.890 --> 04:48.800
inside the parentheses we use the 11.

60
04:48.800 --> 04:56.210
And then we do cards.append() so that we end up adding a 1 instead of the 11.

61
04:56.510 --> 05:00.650
This way we remove the 11 and we replace it with a 1,

62
05:00.920 --> 05:05.390
and hopefully, our cards will now take the user to below 21.