WEBVTT

0
00:00.470 --> 00:03.410
At this point we've created two while loops.

1
00:03.410 --> 00:09.560
The first while loop is for the user so that they can continue taking cards, and the second while loop

2
00:09.560 --> 00:13.760
is for the computer so that the dealer can follow their strategy.

3
00:13.790 --> 00:18.950
Now it's time to tackle Hint 13, where we create a function called compare().

4
00:19.910 --> 00:27.500
We pass in the final user_score and computer_score, and then we compare the scores to see who has won,

5
00:27.500 --> 00:30.480
who has lost, or if they have a draw.

6
00:31.470 --> 00:36.780
So remember that in order to call a function, it has to be declared before the line

7
00:36.780 --> 00:37.860
we want to use it.

8
00:37.860 --> 00:43.110
So let's go back up and let's add our compare() function here.

9
00:43.200 --> 00:49.710
This compare() function is going to take the user_score and also the computer_score,

10
00:49.710 --> 00:58.210
and it's going to compare these two scores if the user_score is equal to the computer_score, then we

11
00:58.210 --> 01:01.600
said this should mean that it's a draw.

12
01:01.810 --> 01:06.520
So we'll return some sort of message that represents this.

13
01:06.910 --> 01:13.360
But elif, or else if the computer has a Blackjack,

14
01:13.930 --> 01:16.630
so the computer_score is equal to 0,

15
01:17.320 --> 01:24.140
well, in this case, we should return that the user has lost and the opponent has a Blackjack.

16
01:24.200 --> 01:27.860
And then we go through this list and we add in all of these other conditions.

17
01:27.860 --> 01:29.270
So I'm going to speed it up a bit.

18
01:29.270 --> 01:31.310
Now for the final condition,

19
01:31.430 --> 01:35.330
it says that the player with the higher score should win.

20
01:35.330 --> 01:42.470
So we can write an elif that compares the user_score against the computer_score,

21
01:42.470 --> 01:49.290
and we're basically saying if the user_score is greater than the computer_score, well, then that means

22
01:49.290 --> 01:50.940
that the user wins.

23
01:52.140 --> 01:59.520
But if that's not true, and because we've covered everything else, we now have the final return,

24
01:59.520 --> 02:01.260
which is, "You lose".

25
02:03.390 --> 02:12.340
Now remember that if, elif, else statements are evaluated from top to bottom, even though if the computer

26
02:12.340 --> 02:18.100
scores a 0, which is a Blackjack, which is obviously much less than whatever score the user might

27
02:18.100 --> 02:24.430
have, because it's going to return at this point and it's going to exit the function,

28
02:24.430 --> 02:29.170
it will never get to this comparison if this is already true.

29
02:29.440 --> 02:36.680
If you get confused about how if , elif, and else works, especially the order in which you add the conditions,

30
02:36.680 --> 02:40.760
have a look back at the lesson where we talked about this in more detail.

31
02:40.760 --> 02:43.760
This might be a good time to revise that concept.

32
02:44.330 --> 02:51.740
Now, another warning that we see in our compare() function is the underlines under the user_score and

33
02:51.740 --> 02:52.820
computer_score.

34
02:52.850 --> 02:59.280
And if we hover over them it says, "It shadows the name 'user_score' from outer scope."

35
02:59.280 --> 03:00.120
What does that mean?

36
03:00.120 --> 03:07.380
Well, it means that we've decided to use the same name for our variable to be used inside our function

37
03:07.380 --> 03:12.840
as the name of a variable that we created outside that function.

38
03:13.470 --> 03:15.660
So how can we prevent this?

39
03:15.660 --> 03:19.710
Well, we have to rename the element as it suggests.

40
03:19.710 --> 03:27.970
Now if we click on this then it opens up the Rename window which is going to find everything that's

41
03:27.970 --> 03:33.910
inside this file to change that variable name.

42
03:33.910 --> 03:38.710
So let's say that I decide to change it to u_score and Refactor,

43
03:38.710 --> 03:45.070
you'll notice that everywhere inside that function where I used that variable name has been renamed

44
03:45.070 --> 03:55.190
automatically for me and the other variables which are unrelated to this particular usage are untouched.

45
03:55.190 --> 04:01.970
And this is a really nice way to rename things if you ever want to, and you can also get to that manually

46
04:01.970 --> 04:06.140
by right-clicking on it, Refactor and then rename.

47
04:06.350 --> 04:09.770
And now you can name it to whatever it is you want.

48
04:09.770 --> 04:13.710
So in my case I'm just going to call it c_score for computer_score.

49
04:13.710 --> 04:15.210
And then I'm going to hit Enter,

50
04:15.210 --> 04:18.570
and all of those changes will be enacted.

51
04:19.740 --> 04:26.580
But now that we've completed our compare() function, we're going to use it at the end of our code here,

52
04:26.580 --> 04:31.980
because this is the time that the user is done drawing cards, the computer is done drawing cards,

53
04:31.980 --> 04:42.370
and now we can call that compare() function and pass in the user_score and also the computer_score.

54
04:43.360 --> 04:49.300
So this should hopefully give us the final result as a piece of text.

55
04:49.540 --> 04:52.210
Let's run the code as it is and see what happens.

56
04:52.210 --> 04:57.460
So we start out with a 3 and a 10 making a score of 13.

57
04:57.460 --> 05:03.140
We can see the computer's first card is a 3 and we're asked whether we want another card,

58
05:03.140 --> 05:08.270
so I'm going to type 'y' so that we get another card and closer to 21.

59
05:08.270 --> 05:12.860
And now we have 3, 10 and 6 making 19.

60
05:12.860 --> 05:18.440
So I think I'm going to say no to pass and hope that I win.

61
05:19.010 --> 05:25.220
So we can probably give the user a little bit more information, like revealing the final deck, what

62
05:25.220 --> 05:27.520
the computer got and what we got.

63
05:27.760 --> 05:31.990
I'm going to go ahead and create some other print statements above this one.

64
05:32.320 --> 05:38.920
Maybe I'll tell the user that "Your final hand is {user_card},

65
05:39.640 --> 05:43.180
final score {user_score}.

66
05:43.870 --> 05:47.620
And then let's reveal to them the computer_score.

67
05:49.480 --> 05:56.470
And then finally we can print the user_score and the computer_score to tell them who won, who lost,

68
05:56.470 --> 05:57.880
or whether if it's a draw.

69
05:59.650 --> 06:02.950
So in this deck we've got 2 and 8 making 10,

70
06:02.950 --> 06:04.900
so I'm going to add another card.

71
06:04.930 --> 06:09.160
Now we're on 16 and I'm going to try and add another card again.

72
06:09.160 --> 06:12.670
And now I'm on 26,

73
06:12.670 --> 06:15.340
so the game is very much over.

74
06:15.340 --> 06:22.040
And it tells me my final hand has a score of 26, the computer's final hand has a score of 20 and I

75
06:22.040 --> 06:22.670
lose.

76
06:23.450 --> 06:29.480
If we wanted to play another game because we are Blackjack addicts, then we want to give the user some

77
06:29.480 --> 06:31.520
sort of means of doing this, right?

78
06:31.520 --> 06:34.580
And that's what the last hint addresses.

79
06:34.580 --> 06:38.060
So we're going to ask the user if they want to restart the game,

80
06:38.060 --> 06:43.770
if the answer is 'yes', then we're going to clear the console and start a new game of Blackjack and also

81
06:43.770 --> 06:46.230
show the logo from art.py,

82
06:46.230 --> 06:53.820
while we're at it. Let's go ahead and add an input, and we can ask them, "Do you want to play a game

83
06:53.820 --> 06:56.970
of Blackjack? Type yes or no."

84
06:57.660 --> 07:01.950
So remember that this entire function is going to be evaluated,

85
07:01.950 --> 07:07.310
and the result or the output is going to replace this part of the code.

86
07:07.370 --> 07:14.180
So we can say that while the input is equal to y, then we're going to keep playing the game.

87
07:14.180 --> 07:23.090
And the game is all the code between this part where we create our empty variables and where we do our

88
07:23.090 --> 07:24.230
comparison.

89
07:25.610 --> 07:33.050
So if we actually put this inside a function by indenting it over and then creating a function here

90
07:33.050 --> 07:42.140
called play_game(), well then we can actually call this function play game every time the user types

91
07:42.140 --> 07:42.980
y.

92
07:45.260 --> 07:49.580
Now we don't want to clutter up our screen with all the previous games,

93
07:49.580 --> 07:57.090
so before we call play_game() we also want to clear the console. And to do that, we're going to use that

94
07:57.090 --> 08:04.110
trick that we saw from previous lessons, which is where we print the \n, the new line character,

95
08:04.110 --> 08:06.480
and we multiply it by 20 times.

96
08:06.510 --> 08:13.500
And then we can call play_game() so that the code in this function gets repeated.

97
08:14.190 --> 08:20.840
And we also said that at the beginning when we play_game() we're going to show the user the logo from

98
08:20.840 --> 08:21.800
art.py.

99
08:21.950 --> 08:25.190
So let's go back and import that as well.

100
08:25.190 --> 08:28.550
So from art import logo.

101
08:29.570 --> 08:34.760
And then at the beginning of every game we're going to print our logo.

102
08:35.270 --> 08:38.990
That's all there is to this project.

103
08:38.990 --> 08:43.970
And now let's run it one final time and see the whole thing in action.

104
08:44.900 --> 08:47.510
"Do you want to play a game of Blackjack?

105
08:47.540 --> 08:49.100
Type y or n?"

106
08:49.100 --> 08:52.370
So yes, we've got our logo.

107
08:54.470 --> 08:59.780
And then it says type 'y' to get another card and type 'n' to pass.

108
08:59.780 --> 09:01.280
So we're on 19,

109
09:01.280 --> 09:03.680
that's pretty tenuous.

110
09:03.680 --> 09:08.240
I'm going to pass and I end up with a score of 19,

111
09:08.240 --> 09:16.690
the computer, unfortunately, has gone over 21, so therefore, I win. And we can keep repeating this to

112
09:16.690 --> 09:18.970
play more games of Blackjack.

113
09:18.970 --> 09:21.940
So I hope you had fun at building this game with me.

114
09:21.940 --> 09:27.460
I know this was a really difficult project, and I know that some of you would have probably had some

115
09:27.460 --> 09:33.160
issues, but I really hope that you persevered and you got through it in the end and through the process,

116
09:33.160 --> 09:37.570
you've managed to apply a lot of the things that we've learned in the last ten days.

117
09:37.660 --> 09:42.970
This is a good time to review the concepts you've learned, and have a think about what you might need

118
09:42.970 --> 09:50.230
to revise or revisit so that you're really, really strong on all the concepts and you're comfortable

119
09:50.230 --> 09:52.720
to continue on to the next ten days.

120
09:53.350 --> 10:00.290
Now, if you want to take a look at the solution code I've got here, then simply head over to this solution.py,

121
10:00.290 --> 10:06.680
and you will see everything that I've written in here for you to be able to browse and refer

122
10:06.680 --> 10:07.220
to.

123
10:08.480 --> 10:10.250
So now rest up.

124
10:10.250 --> 10:16.130
Well done for finishing this day and if you haven't completed it, don't worry, there's always another

125
10:16.130 --> 10:16.880
day tomorrow.

126
10:16.880 --> 10:22.700
You can always come back and eventually, with deep perseverance, you will get there in the end.

127
10:22.730 --> 10:23.990
I believe in you.