1
00:00:00,120 --> 00:00:03,090
All right. So now let's tackle hint number 6.

2
00:00:03,660 --> 00:00:06,870
We're going to create a function called calculate_score

3
00:00:06,900 --> 00:00:09,570
that's going to take a list of cards as an input,

4
00:00:09,990 --> 00:00:13,110
and then it returns the score after calculation.

5
00:00:13,890 --> 00:00:16,050
And it tells us that if we get stuck,

6
00:00:16,110 --> 00:00:18,420
we can look up the sum function to help us.

7
00:00:19,080 --> 00:00:22,440
So let's create a function called calculate_score

8
00:00:23,190 --> 00:00:27,720
and this calculate_score is going to take some cards as an input,

9
00:00:28,350 --> 00:00:29,790
and then inside the body

10
00:00:29,820 --> 00:00:34,670
it's going to calculate the total of all the cards in this list.

11
00:00:35,360 --> 00:00:38,780
So if we take a look at the sum function in Python,

12
00:00:39,260 --> 00:00:43,100
the way that it works is you can put a iterable,

13
00:00:43,220 --> 00:00:47,930
so like a list inside the parentheses as an input

14
00:00:48,440 --> 00:00:52,310
and then it's going to add up all of the items in the list

15
00:00:52,640 --> 00:00:56,570
and it's going to return the total. Back in our code

16
00:00:56,870 --> 00:01:00,950
we can simply write sum, and then inside these parentheses

17
00:01:00,980 --> 00:01:05,980
we can pass over the cards and we can return this value as the output.

18
00:01:07,760 --> 00:01:08,450
Basically

19
00:01:08,450 --> 00:01:13,310
if we had cards equal to a list with 1, 5,

20
00:01:13,340 --> 00:01:14,173
3,

21
00:01:14,180 --> 00:01:19,180
4, then the sum function is going to end up being equal to 1 + 5 +

22
00:01:19,580 --> 00:01:23,060
3 + 4, which is going to be equal to 13.

23
00:01:23,360 --> 00:01:26,450
And then that will become the output of this function.

24
00:01:27,530 --> 00:01:29,660
So now let's move on to hint 7.

25
00:01:30,710 --> 00:01:32,660
A Blackjack only happens when

26
00:01:32,740 --> 00:01:37,740
we have a hand with only two cards and those cards have to be an Ace and a 10

27
00:01:39,470 --> 00:01:42,470
value card. In our deck

28
00:01:42,500 --> 00:01:47,500
we know that an Ace is represented by 11 and we have a number of 10 value cards.

29
00:01:50,570 --> 00:01:54,410
So there's a number of ways that we can check for this Blackjack.

30
00:01:54,980 --> 00:01:59,980
One of the ways is by checking to see if the 11 is in our deck of cards,

31
00:02:02,180 --> 00:02:07,180
and then we can combine that with an and to check if the 10 is also in our deck

32
00:02:07,850 --> 00:02:09,800
of cards. And finally,

33
00:02:09,830 --> 00:02:13,730
we can check to make sure that we've got a hand size of two.

34
00:02:14,240 --> 00:02:19,070
So we can check that the length of our cards is equal to two,

35
00:02:19,790 --> 00:02:24,790
and this would represent this logic: a hand with only two cards, an Ace

36
00:02:25,220 --> 00:02:29,900
which is represented by 11 and a 10. Now, on the other hand

37
00:02:29,930 --> 00:02:34,930
we could actually also simplify this because a hand with only two cards that

38
00:02:35,630 --> 00:02:38,600
must contain an 11 and a 10, we can

39
00:02:38,600 --> 00:02:42,380
instead of checking for both 11 and 10, we can check to

40
00:02:42,410 --> 00:02:44,390
see if the total,

41
00:02:44,420 --> 00:02:49,420
so that if we summed the cards and if this was equal to 21,

42
00:02:50,750 --> 00:02:55,010
then it would be exactly the same thing, right? 10 + 11 is 21

43
00:02:55,490 --> 00:02:59,740
and we've still only got a deck of two. In this case

44
00:02:59,770 --> 00:03:04,600
what we want to happen is we want to return zero instead of the actual score.

45
00:03:05,110 --> 00:03:10,110
So this way we can indicate that the user or the computer has got a score of

46
00:03:10,720 --> 00:03:15,010
Blackjack. That's hint number 7 sorted,

47
00:03:15,190 --> 00:03:20,110
let's move onto a hint number 8. It tells us that inside calculate_score,

48
00:03:20,320 --> 00:03:23,770
we should check also for an 11, which is an Ace.

49
00:03:24,400 --> 00:03:26,620
If the score is already over 21,

50
00:03:27,010 --> 00:03:32,010
then we should remove the 11 from the cards and we could replace it with a one,

51
00:03:32,920 --> 00:03:36,940
because remember that an Ace can count as an 11 or 1.

52
00:03:37,420 --> 00:03:41,140
So in the beginning, when the user's score is very small,

53
00:03:41,380 --> 00:03:43,840
then we probably want to count it as an 11.

54
00:03:44,260 --> 00:03:46,690
But once the user goes over 21,

55
00:03:46,960 --> 00:03:50,470
then we probably want to change it and count it as a 1 instead

56
00:03:50,500 --> 00:03:55,000
so they don't go over and they don't lose. Let's first write

57
00:03:55,000 --> 00:03:55,870
the if statement.

58
00:03:56,140 --> 00:04:01,140
So if there is the 11 inside our deck of cards and the

59
00:04:03,670 --> 00:04:08,650
sum of the cards, so the total_score, is already over 21,

60
00:04:09,220 --> 00:04:11,140
then we want to do something about it.

61
00:04:11,680 --> 00:04:15,550
And the hint tells us to look up the append and remove functions.

62
00:04:16,089 --> 00:04:19,180
So we've already seen the append function many times,

63
00:04:19,360 --> 00:04:23,500
and we know that it just adds a single element to the end of the list.

64
00:04:24,520 --> 00:04:28,180
Now the one that we haven't used a lot of is the remove function

65
00:04:28,750 --> 00:04:33,750
and this we'll search for the first instance of the given element and removes it

66
00:04:34,870 --> 00:04:38,260
out of the list. This is kind of what we need to do.

67
00:04:38,590 --> 00:04:43,360
We need to go ahead and remove the 11 from the list of cards. So we can write

68
00:04:43,360 --> 00:04:47,080
cards.remove, and inside the parentheses,

69
00:04:47,380 --> 00:04:52,030
we use the 11 and then we do cards.append,

70
00:04:52,360 --> 00:04:55,960
so that we end up adding a 1 instead of the 11.

71
00:04:56,740 --> 00:05:00,310
This way we remove the 11 and replace it with a 1.

72
00:05:01,210 --> 00:05:05,140
And hopefully our cards will now take the user to below 21.

