WEBVTT

00:00.050 --> 00:05.510
All right, guys, so now that we've loaded up all the knowledge that we need into our brains, it's

00:05.510 --> 00:08.870
time to tackle the final project of the day.

00:08.870 --> 00:14.360
And as I showed you in the beginning of the day, it's a rock, paper, scissors game that we can play

00:14.360 --> 00:15.590
with the computer.

00:15.680 --> 00:22.130
So the game starts out by asking you to type zero for rock, one for paper or two for scissors.

00:22.130 --> 00:25.070
So let's go ahead and type two for scissors.

00:25.100 --> 00:29.900
And then it's going to show you a graphic of your choice scissors.

00:29.900 --> 00:33.440
And then the choice the computer made which is rock.

00:33.470 --> 00:35.600
And of course rock beats scissors.

00:35.600 --> 00:37.040
So you lose.

00:37.070 --> 00:44.720
What this game boils down to is some way of randomly making a choice between rock, paper, or scissors,

00:44.720 --> 00:51.080
and then comparing that choice that the computer randomly generated against your choice.

00:51.080 --> 00:57.050
And then based on the rules of rock, paper and scissors, determining whether if you won, you lost,

00:57.050 --> 01:00.050
or whether if you had a drawer.

01:00.710 --> 01:08.060
While I was looking around, I actually came across the world Rock paper Scissors Association and they

01:08.060 --> 01:12.800
have the official rules, um, listed of rock, paper and scissors.

01:12.830 --> 01:17.240
If you're not familiar with this game, it might be worth checking out this website.

01:17.270 --> 01:20.180
Here are the three shapes of rock, paper and scissors.

01:20.180 --> 01:24.650
And then the rules are that rock wins against scissors.

01:24.680 --> 01:28.670
Scissors wins against paper and paper wins against rock.

01:29.390 --> 01:35.930
Now you'll notice that in the starting project, I've already got the Ascii art for rock, paper and

01:35.930 --> 01:39.950
scissors in here, and they each saved to a variable.

01:39.950 --> 01:47.300
So what that means is that you can start out by just having a go at printing out, let's say, a rock,

01:47.480 --> 01:49.910
and remember that these are variable names.

01:49.910 --> 01:53.420
They're not strings, so they don't need the double quotes around them.

01:53.420 --> 02:00.540
So let's run that and you can see what we get printed is the particular Ascii art that I chose in my

02:00.540 --> 02:01.560
print statement.

02:01.890 --> 02:04.830
So this is already yours.

02:04.830 --> 02:11.460
But as you'll notice, there's no other logic that I've provided you, and you are going to rely on

02:11.460 --> 02:16.980
what you've learned in the previous days and most importantly, what you've learned in today's lessons

02:16.980 --> 02:19.080
to be able to complete this challenge.

02:19.080 --> 02:25.440
And it's worth comparing the outcome against the game of rock, paper, scissors that I'll link to.

02:25.770 --> 02:30.930
There's a couple of things to think about, namely, how are you going to decide who won or who lost?

02:30.960 --> 02:38.010
How are you going to get the computer to choose a random shape rock, paper or scissors and how you

02:38.010 --> 02:43.440
actually get this game to work in the same way that is demoed in this final version?

02:43.950 --> 02:51.780
I'm going to go quiet now, and I'm going to let you pause the video, and I want you to spend at least

02:51.780 --> 02:53.910
10 or 15 minutes working on this.

02:53.910 --> 02:59.100
And if you get stuck, just try some things out or maybe watch some of the previous videos in the day

02:59.100 --> 03:01.620
and just try to give it your best.

03:01.620 --> 03:04.140
Go and do a lot of struggling.

03:04.170 --> 03:08.850
Hopefully you're going to succeed, and once you're done, head back over here and I'll go through the

03:08.850 --> 03:09.840
solution with you.

03:09.840 --> 03:11.400
So pause the video now.

03:11.790 --> 03:14.940
A3 all right.

03:14.940 --> 03:17.550
So we know that we've got all of this Ascii art.

03:17.550 --> 03:24.180
But for the moment I'm just going to start off by pinning down the basic logic of this game.

03:24.180 --> 03:30.660
And it's really helpful for you as well if you start thinking about breaking down the larger problem,

03:30.660 --> 03:36.720
which is making the rock, paper, scissors game into smaller problems that you can solve, like generating

03:36.720 --> 03:39.060
a random number between 1 and 3.

03:39.060 --> 03:42.510
So we have some sort of proxy for rock, paper and scissors.

03:42.510 --> 03:45.450
And then maybe thinking about putting down the logic.

03:45.450 --> 03:51.330
Well, if the computer chose scissors and I chose paper, then I'm going to lose.

03:51.330 --> 03:56.170
Or if the computer chose rock and I chose paper, then I'm going to win.

03:56.170 --> 04:02.770
And putting that down on a flow chart using Draw.io or something like that can make this challenge a

04:02.770 --> 04:04.450
lot easier as well.

04:05.140 --> 04:11.170
But without further ado, the first thing I need to do is to produce a user choice.

04:11.200 --> 04:17.140
Now this is going to be the one that the user chose when they typed in a value.

04:17.170 --> 04:21.940
And in order to get the value from them, I'm going to need to use a input.

04:21.970 --> 04:29.350
And inside the input I'm going to put the following prompt asking the user what do you choose?

04:29.380 --> 04:33.280
Type zero for rock, one for paper or two for scissors.

04:33.310 --> 04:39.940
Now once I get hold of this input, hopefully if the user is following my instructions, they're going

04:39.940 --> 04:43.720
to type some sort of number, either 0 or 1 or two.

04:43.750 --> 04:49.960
Now that I've gotten hold of what the user wants to choose, the next thing to do is figure out what

04:49.960 --> 04:51.640
the computer is going to choose.

04:51.670 --> 04:58.010
So I'm going to make another variable called computer choice and I'm going to generate a random number.

04:58.040 --> 05:02.210
So to do that remember we have to import the random module.

05:03.500 --> 05:11.720
And then we can start using it to generate random whole numbers by using random dot randint.

05:11.720 --> 05:16.520
And then the range is going to be between 0 and 2.

05:16.550 --> 05:18.950
Because this is what I asked the user to do.

05:18.980 --> 05:22.160
Type zero for rock, one for paper, two for scissors.

05:22.160 --> 05:27.020
So the computer is also going to choose between zero, 1 or 2.

05:27.620 --> 05:33.680
Now that I've got the computer choice, I'm actually going to just print it out for now instead of printing

05:33.680 --> 05:37.610
out the actual shape, I'm just going to print out the number.

05:37.610 --> 05:40.700
So computer chose.

05:40.700 --> 05:43.790
And then let's go ahead and add a F string.

05:45.620 --> 05:49.460
And of course that requires the f in front of the string.

05:49.760 --> 05:55.160
So now if we just play the game as it is, remember that testing your app while you're developing it

05:55.160 --> 05:57.020
is really, really good practice.

05:57.050 --> 06:02.900
It picks up on the bugs that you make along the way, instead of waiting until the end when you've done

06:02.900 --> 06:07.610
everything and then it doesn't work, and you have to untangle all of the lines of code.

06:07.790 --> 06:13.010
At this point, I've already realized that it's actually not so great getting the user to type on this

06:13.010 --> 06:13.730
line.

06:14.150 --> 06:19.070
What I would much rather is to start a new line for them to type their answer on.

06:19.100 --> 06:22.850
So if I run the code again, then it should look a bit like this.

06:22.850 --> 06:24.140
My prompt is now here.

06:24.140 --> 06:27.050
So I'm going to choose zero for rock.

06:27.050 --> 06:30.860
And then it tells me that computer also chose zero.

06:30.860 --> 06:33.020
So in this case this would be a draw.

06:33.020 --> 06:38.540
But if I played this game again, you can see that the computer is probably going to choose something

06:38.540 --> 06:39.170
different.

06:39.170 --> 06:40.640
So I chose rock again.

06:40.670 --> 06:43.250
The computer chose one, which is paper.

06:43.460 --> 06:51.710
So now we can start thinking about how do we compare these two choices, and how can we define the logic

06:51.710 --> 06:53.750
of rock, paper, scissors.

06:53.780 --> 07:01.250
So I know that paper beats rock, scissors beats paper, and rock will beat scissors.

07:01.250 --> 07:10.190
So if zero is rock, one is paper and two is scissors, then two beats one and one beats zero.

07:10.190 --> 07:16.790
So that's very simple for us to check in terms of using if statements.

07:16.820 --> 07:17.030
Right.

07:17.060 --> 07:19.010
We could simply just check.

07:19.010 --> 07:29.480
Well, if the computer choice is greater than the user choice, well then this probably means that the

07:29.480 --> 07:31.490
computer wins.

07:32.300 --> 07:36.050
But this is not true in all cases, right?

07:36.050 --> 07:43.880
Because if the computer chose two for scissors and the user chose zero for rock, then the user should

07:43.880 --> 07:46.130
win rather than the computer winning.

07:46.130 --> 07:51.080
So we actually have some exceptions to this rule.

07:51.410 --> 07:57.740
How can we catch the exceptions before we go down to this level of generalization?

07:57.770 --> 08:00.920
Well, we could, instead of using this as an.

08:00.950 --> 08:09.200
If we use this as an Elif and we create another if statement which says, well, if the user choice

08:09.230 --> 08:11.120
is equal to zero.

08:11.120 --> 08:18.260
So if the user chose rock and the computer choice was two.

08:18.290 --> 08:23.270
Well then this means that in this case the user actually wins.

08:24.500 --> 08:29.960
Now, if we're thinking from the perspective of the user when they're playing this game, then they

08:29.960 --> 08:33.440
don't really care if the computer won or the user won.

08:33.440 --> 08:36.470
They want to know did they win or did they lose.

08:36.470 --> 08:38.990
So let's change this wording a little bit.

08:39.020 --> 08:44.060
We'll say instead of user wins we'll say you win.

08:44.510 --> 08:49.850
And if the computer wins then we'll say you lose.

08:49.850 --> 08:49.860
News.

08:50.160 --> 08:54.960
So now if we run our program and test it out.

08:54.960 --> 08:58.470
So let's say I'm going to type zero for rock.

08:58.680 --> 09:00.600
But we actually get an error.

09:00.600 --> 09:08.940
It tells us that the greater than comparison is not supported between instances of int and string.

09:08.940 --> 09:10.170
What's going on here.

09:10.170 --> 09:14.760
Well remember that we get our input as a number.

09:14.760 --> 09:19.020
But the input is always going to be a string.

09:19.020 --> 09:20.820
So we want this to be a number.

09:20.820 --> 09:24.210
Then we're going to have to wrap it inside an int.

09:24.210 --> 09:27.210
So now we turn it into a whole number.

09:27.810 --> 09:29.820
Now this might be a good point to address.

09:29.820 --> 09:34.170
Well what should happen if they typed something that wasn't zero 1 or 2?

09:34.200 --> 09:36.090
What if they typed 34?

09:36.120 --> 09:39.090
Well then naturally they should probably lose, right?

09:39.090 --> 09:43.200
So we can add an else statement addressing this situation.

09:43.200 --> 09:47.160
So we could say else we just print.

09:47.190 --> 09:50.520
You typed an invalid number.

09:50.730 --> 09:51.930
You lose.

09:52.920 --> 09:53.460
Cool.

09:53.460 --> 09:54.840
So that sorts that out.

09:54.870 --> 09:59.100
Now let's run our code again and see if it works.

09:59.130 --> 09:59.910
What do you choose?

09:59.940 --> 10:01.050
Type zero for ROC.

10:01.080 --> 10:02.640
I'm going to choose ROC again.

10:02.670 --> 10:08.130
Computer chose zero and it tells me that you typed an invalid number.

10:08.640 --> 10:11.910
Well, actually zero is not an invalid number.

10:11.910 --> 10:13.470
So what's going on here?

10:13.500 --> 10:17.250
Well, it's because it didn't fit this criteria.

10:17.340 --> 10:24.900
So I had zero, computer had zero, and it didn't fit this criteria because zero is not greater than

10:24.930 --> 10:25.710
zero.

10:25.710 --> 10:29.940
So it basically defaulted to the final else statement.

10:30.240 --> 10:34.620
But what's actually happening here is that that was a draw.

10:34.650 --> 10:35.970
Computer chose ROC.

10:36.000 --> 10:36.930
I chose ROC.

10:36.960 --> 10:42.090
That's a draw in the official rules of Rock, paper, scissors.

10:42.240 --> 10:44.610
So how do I catch that?

10:44.610 --> 10:51.550
Well, I could create another Elif statement which says, well, if the computer choice is equal to

10:51.580 --> 10:52.690
the user choice.

10:52.720 --> 10:54.910
Well, then we're going to print.

10:55.270 --> 10:56.950
It's a draw.

10:57.820 --> 11:03.580
Now let's test our app again and see if there are any other situations where our code is not behaving

11:03.580 --> 11:04.600
properly.

11:04.780 --> 11:06.880
I'm going to choose rock again.

11:06.910 --> 11:09.400
Computer chose rock as well.

11:09.400 --> 11:11.050
And I get it's a draw.

11:11.050 --> 11:12.490
So that's pretty good.

11:13.780 --> 11:20.530
Now if I choose zero and the computer chose two, then this is going to be the statement that gets triggered

11:20.530 --> 11:21.820
and I win.

11:21.820 --> 11:23.350
So that's pretty good.

11:23.680 --> 11:30.310
Now what if I chose two for scissors and the computer chose zero.

11:30.640 --> 11:34.120
Well in this case rock beats scissors.

11:34.120 --> 11:37.000
So it should actually say I lose.

11:37.000 --> 11:40.360
But instead it says you typed an invalid number.

11:40.360 --> 11:43.150
So it defaulted to the else statement.

11:43.150 --> 11:49.360
Again, because this particular situation is not caused by any of these statements.

11:49.870 --> 11:51.760
So what do we have to do instead?

11:51.790 --> 12:01.480
Well, we actually need another Elif to say, well, if the computer choice was equal to zero and the

12:01.480 --> 12:03.790
user choice was equal to two.

12:03.820 --> 12:11.770
Well, this means that rock beats scissors, so I lose and the computer wins.

12:11.950 --> 12:16.300
Now, the final one that we need to catch is this case.

12:16.330 --> 12:25.660
If I type one for paper and computer chooses zero for rock, it again defaults to the else statement.

12:25.780 --> 12:30.130
Whereas in fact in this case it should tell me that I won.

12:30.160 --> 12:31.870
So what's missing here?

12:31.900 --> 12:35.980
Well, it's the partner to this particular statement.

12:36.100 --> 12:40.450
If computer choice is greater than user choice, then I lose.

12:40.450 --> 12:46.630
But on the other hand, if the user choice was greater than the computer choice.

12:46.660 --> 12:49.360
Then I actually should win.

12:51.340 --> 12:57.430
So now the final thing we might have to fix is this else statement.

12:57.460 --> 13:03.160
Now actually never gets called because this is what happens.

13:03.160 --> 13:06.610
So let's say that I choose 456.

13:06.640 --> 13:09.820
It tells me that computer chose to I win.

13:09.850 --> 13:10.870
What's happening here?

13:10.900 --> 13:15.910
Well, it's actually this particular line that's being carried out.

13:15.910 --> 13:18.490
User choice is greater than computer choice.

13:18.520 --> 13:19.990
This is not what we want.

13:20.020 --> 13:22.000
We want it to default to.

13:22.030 --> 13:23.590
You typed in invalid number.

13:23.590 --> 13:24.610
You lose.

13:24.610 --> 13:28.840
So we can't actually just use else in this situation.

13:28.870 --> 13:31.870
We actually need to provide a condition.

13:31.900 --> 13:40.930
So we're going to use Elif to check if the user choice is greater or equal to three.

13:41.200 --> 13:45.800
Now, the computer will never choose anything other than zero, one, and two.

13:45.830 --> 13:52.250
So we don't have to check that one, but the user could choose something above three or below zero,

13:52.250 --> 14:00.740
so we can use an Or statement to catch this, or if the user choice is less than zero.

14:00.830 --> 14:03.980
Well, in this case you typed an invalid number.

14:03.980 --> 14:05.210
You lose.

14:05.480 --> 14:08.210
But that's not all that we have to do.

14:08.210 --> 14:16.430
If we hit run and we again type some extraordinarily large number, you'll see that it still says computer

14:16.430 --> 14:17.210
chose one.

14:17.210 --> 14:18.200
You win.

14:18.230 --> 14:19.190
What's going on.

14:19.190 --> 14:22.610
We have this statement that should catch this situation.

14:22.610 --> 14:25.310
But it's right at the bottom.

14:25.310 --> 14:30.560
So it's not going to be checked until one of the previous ones are checked.

14:30.560 --> 14:34.910
Namely it's one of these that's going to be the problem.

14:34.910 --> 14:43.250
So what we have to do is we have to move this statement above all the lines so that we first check if

14:43.250 --> 14:48.590
the user choice is greater than three or less than zero.

14:48.770 --> 14:55.040
And if that's not true, do we actually continue checking to see who won the game?

14:55.370 --> 14:58.460
Now finally we can run our code.

14:58.490 --> 15:02.000
Type in invalid number and get you typed in invalid number.

15:02.030 --> 15:02.960
You lose.

15:03.770 --> 15:10.850
So now that we've actually got our logic all sorted out, the next step is to include our images.

15:10.880 --> 15:14.810
And to do that, we have to somehow match it up with these numbers.

15:14.810 --> 15:17.210
We have right zero, one and two.

15:17.240 --> 15:22.970
Now there's many, many ways that you could do this and you could solve this project.

15:22.970 --> 15:31.460
But the easiest way is probably putting the game images into a list where we have rock as the first

15:31.460 --> 15:36.350
one, paper as the second one, and scissors as the last one.

15:36.350 --> 15:43.350
So we're using the fact that lists have a Order that is always going to be followed.

15:43.350 --> 15:47.370
So we can now get hold of the rock picture by getting game images.

15:47.400 --> 15:48.120
Zero.

15:48.150 --> 15:55.500
We can get scissors by doing game images two and so on and so forth, and we can match those up to our

15:55.500 --> 15:58.470
choices, the user choice and the computer choice.

15:58.470 --> 16:06.000
So when the user chooses a number, then we're going to print from the game images.

16:06.000 --> 16:12.510
And we're going to use our little square brackets to select the image we want from this list.

16:12.510 --> 16:16.710
And the one that we want is going to be based on the user's choice.

16:16.740 --> 16:17.190
Right?

16:17.220 --> 16:19.680
Zero for rock, one for paper, two for scissors.

16:19.680 --> 16:26.640
So we're going to put user choice as the index to pick out an image from game images.

16:27.120 --> 16:31.320
The index can only be zero, 1 or 2.

16:31.350 --> 16:38.910
So we should only print the image if our user picked a number that's greater or equal to zero and less

16:38.910 --> 16:41.220
than or equal to two.

16:41.370 --> 16:48.000
Otherwise, our program will crash before we reach our checking code down here, where we tell our user

16:48.000 --> 16:50.910
that they've typed in an invalid number.

16:51.780 --> 16:58.920
Now, down here for the computer choice, instead of printing the number that the computer chose, I'm

16:58.920 --> 17:02.640
going to delete that and add a little colon.

17:03.480 --> 17:07.590
So that way it's going to show computer chose.

17:07.590 --> 17:13.140
And then I'm going to print the image of the choice that the computer made.

17:13.170 --> 17:15.300
So I'm going to add another print statement.

17:15.300 --> 17:17.370
I'm going to tap into the game images.

17:17.370 --> 17:23.790
And then inside some square brackets I'm going to use the computer choice which is going to be between

17:23.790 --> 17:30.240
zero, one and two as the index to pick out the corresponding image from my list.

17:30.270 --> 17:31.500
Game images.

17:31.530 --> 17:38.310
So now we're finally ready to run our code, and I'm going to choose zero for rock and the computer

17:38.310 --> 17:39.910
to chose paper.

17:39.910 --> 17:43.090
So paper beats rock, I lose.

17:43.450 --> 17:44.680
Let's try this again.

17:44.680 --> 17:45.040
I'm going.

17:45.070 --> 17:48.670
To choose scissors and computer chose scissors.

17:48.670 --> 17:49.750
It's a draw.

17:49.780 --> 17:58.300
Now we have completed the project by using everything from lists to randomization to variables to if

17:58.300 --> 18:00.970
statements and a whole lot of logic.

18:00.970 --> 18:05.170
So how did you get on with this project if you struggled?

18:05.170 --> 18:10.600
This is the time again to go back to it and maybe map things out using a flow diagram.

18:10.600 --> 18:15.790
But the important thing is that you got to write your own code and you've got to make it work, and

18:15.790 --> 18:20.920
this is the only way that you'll understand what's going on so that you can continue on.

18:20.920 --> 18:26.860
And as things get harder, you'll be able to learn more because you've already understood all of the

18:26.860 --> 18:28.180
previous knowledge.

18:28.210 --> 18:34.720
So I hope you had fun with me today, learning and building, and hopefully I will see you tomorrow

18:34.750 --> 18:35.890
bright and early.

18:35.890 --> 18:38.500
So that's goodnight from me for today.
