All right, guys, so now that we've loaded up all the knowledge that we need into our brains, it's
time to tackle the final project of the day.
And as I showed you in the beginning of the day, it's a rock, paper, scissors game that we can play
with the computer.
So the game starts out by asking you to type zero for rock, one for paper or two for scissors.
So let's go ahead and type two for scissors.
And then it's going to show you a graphic of your choice scissors.
And then the choice the computer made which is rock.
And of course rock beats scissors.
So you lose.
What this game boils down to is some way of randomly making a choice between rock, paper, or scissors,
and then comparing that choice that the computer randomly generated against your choice.
And then based on the rules of rock, paper and scissors, determining whether if you won, you lost,
or whether if you had a drawer.
While I was looking around, I actually came across the world Rock paper Scissors Association and they
have the official rules, um, listed of rock, paper and scissors.
If you're not familiar with this game, it might be worth checking out this website.
Here are the three shapes of rock, paper and scissors.
And then the rules are that rock wins against scissors.
Scissors wins against paper and paper wins against rock.
Now you'll notice that in the starting project, I've already got the Ascii art for rock, paper and
scissors in here, and they each saved to a variable.
So what that means is that you can start out by just having a go at printing out, let's say, a rock,
and remember that these are variable names.
They're not strings, so they don't need the double quotes around them.
So let's run that and you can see what we get printed is the particular Ascii art that I chose in my
print statement.
So this is already yours.
But as you'll notice, there's no other logic that I've provided you, and you are going to rely on
what you've learned in the previous days and most importantly, what you've learned in today's lessons
to be able to complete this challenge.
And it's worth comparing the outcome against the game of rock, paper, scissors that I'll link to.
There's a couple of things to think about, namely, how are you going to decide who won or who lost?
How are you going to get the computer to choose a random shape rock, paper or scissors and how you
actually get this game to work in the same way that is demoed in this final version?
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
10 or 15 minutes working on this.
And if you get stuck, just try some things out or maybe watch some of the previous videos in the day
and just try to give it your best.
Go and do a lot of struggling.
Hopefully you're going to succeed, and once you're done, head back over here and I'll go through the
solution with you.
So pause the video now.
A3 all right.
So we know that we've got all of this Ascii art.
But for the moment I'm just going to start off by pinning down the basic logic of this game.
And it's really helpful for you as well if you start thinking about breaking down the larger problem,
which is making the rock, paper, scissors game into smaller problems that you can solve, like generating
a random number between 1 and 3.
So we have some sort of proxy for rock, paper and scissors.
And then maybe thinking about putting down the logic.
Well, if the computer chose scissors and I chose paper, then I'm going to lose.
Or if the computer chose rock and I chose paper, then I'm going to win.
And putting that down on a flow chart using Draw.io or something like that can make this challenge a
lot easier as well.
But without further ado, the first thing I need to do is to produce a user choice.
Now this is going to be the one that the user chose when they typed in a value.
And in order to get the value from them, I'm going to need to use a input.
And inside the input I'm going to put the following prompt asking the user what do you choose?
Type zero for rock, one for paper or two for scissors.
Now once I get hold of this input, hopefully if the user is following my instructions, they're going
to type some sort of number, either 0 or 1 or two.
Now that I've gotten hold of what the user wants to choose, the next thing to do is figure out what
the computer is going to choose.
So I'm going to make another variable called computer choice and I'm going to generate a random number.
So to do that remember we have to import the random module.
And then we can start using it to generate random whole numbers by using random dot randint.
And then the range is going to be between 0 and 2.
Because this is what I asked the user to do.
Type zero for rock, one for paper, two for scissors.
So the computer is also going to choose between zero, 1 or 2.
Now that I've got the computer choice, I'm actually going to just print it out for now instead of printing
out the actual shape, I'm just going to print out the number.
So computer chose.
And then let's go ahead and add a F string.
And of course that requires the f in front of the string.
So now if we just play the game as it is, remember that testing your app while you're developing it
is really, really good practice.
It picks up on the bugs that you make along the way, instead of waiting until the end when you've done
everything and then it doesn't work, and you have to untangle all of the lines of code.
At this point, I've already realized that it's actually not so great getting the user to type on this
line.
What I would much rather is to start a new line for them to type their answer on.
So if I run the code again, then it should look a bit like this.
My prompt is now here.
So I'm going to choose zero for rock.
And then it tells me that computer also chose zero.
So in this case this would be a draw.
But if I played this game again, you can see that the computer is probably going to choose something
different.
So I chose rock again.
The computer chose one, which is paper.
So now we can start thinking about how do we compare these two choices, and how can we define the logic
of rock, paper, scissors.
So I know that paper beats rock, scissors beats paper, and rock will beat scissors.
So if zero is rock, one is paper and two is scissors, then two beats one and one beats zero.
So that's very simple for us to check in terms of using if statements.
Right.
We could simply just check.
Well, if the computer choice is greater than the user choice, well then this probably means that the
computer wins.
But this is not true in all cases, right?
Because if the computer chose two for scissors and the user chose zero for rock, then the user should
win rather than the computer winning.
So we actually have some exceptions to this rule.
How can we catch the exceptions before we go down to this level of generalization?
Well, we could, instead of using this as an.
If we use this as an Elif and we create another if statement which says, well, if the user choice
is equal to zero.
So if the user chose rock and the computer choice was two.
Well then this means that in this case the user actually wins.
Now, if we're thinking from the perspective of the user when they're playing this game, then they
don't really care if the computer won or the user won.
They want to know did they win or did they lose.
So let's change this wording a little bit.
We'll say instead of user wins we'll say you win.
And if the computer wins then we'll say you lose.
News.
So now if we run our program and test it out.
So let's say I'm going to type zero for rock.
But we actually get an error.
It tells us that the greater than comparison is not supported between instances of int and string.
What's going on here.
Well remember that we get our input as a number.
But the input is always going to be a string.
So we want this to be a number.
Then we're going to have to wrap it inside an int.
So now we turn it into a whole number.
Now this might be a good point to address.
Well what should happen if they typed something that wasn't zero 1 or 2?
What if they typed 34?
Well then naturally they should probably lose, right?
So we can add an else statement addressing this situation.
So we could say else we just print.
You typed an invalid number.
You lose.
Cool.
So that sorts that out.
Now let's run our code again and see if it works.
What do you choose?
Type zero for ROC.
I'm going to choose ROC again.
Computer chose zero and it tells me that you typed an invalid number.
Well, actually zero is not an invalid number.
So what's going on here?
Well, it's because it didn't fit this criteria.
So I had zero, computer had zero, and it didn't fit this criteria because zero is not greater than
zero.
So it basically defaulted to the final else statement.
But what's actually happening here is that that was a draw.
Computer chose ROC.
I chose ROC.
That's a draw in the official rules of Rock, paper, scissors.
So how do I catch that?
Well, I could create another Elif statement which says, well, if the computer choice is equal to
the user choice.
Well, then we're going to print.
It's a draw.
Now let's test our app again and see if there are any other situations where our code is not behaving
properly.
I'm going to choose rock again.
Computer chose rock as well.
And I get it's a draw.
So that's pretty good.
Now if I choose zero and the computer chose two, then this is going to be the statement that gets triggered
and I win.
So that's pretty good.
Now what if I chose two for scissors and the computer chose zero.
Well in this case rock beats scissors.
So it should actually say I lose.
But instead it says you typed an invalid number.
So it defaulted to the else statement.
Again, because this particular situation is not caused by any of these statements.
So what do we have to do instead?
Well, we actually need another Elif to say, well, if the computer choice was equal to zero and the
user choice was equal to two.
Well, this means that rock beats scissors, so I lose and the computer wins.
Now, the final one that we need to catch is this case.
If I type one for paper and computer chooses zero for rock, it again defaults to the else statement.
Whereas in fact in this case it should tell me that I won.
So what's missing here?
Well, it's the partner to this particular statement.
If computer choice is greater than user choice, then I lose.
But on the other hand, if the user choice was greater than the computer choice.
Then I actually should win.
So now the final thing we might have to fix is this else statement.
Now actually never gets called because this is what happens.
So let's say that I choose 456.
It tells me that computer chose to I win.
What's happening here?
Well, it's actually this particular line that's being carried out.
User choice is greater than computer choice.
This is not what we want.
We want it to default to.
You typed in invalid number.
You lose.
So we can't actually just use else in this situation.
We actually need to provide a condition.
So we're going to use Elif to check if the user choice is greater or equal to three.
Now, the computer will never choose anything other than zero, one, and two.
So we don't have to check that one, but the user could choose something above three or below zero,
so we can use an Or statement to catch this, or if the user choice is less than zero.
Well, in this case you typed an invalid number.
You lose.
But that's not all that we have to do.
If we hit run and we again type some extraordinarily large number, you'll see that it still says computer
chose one.
You win.
What's going on.
We have this statement that should catch this situation.
But it's right at the bottom.
So it's not going to be checked until one of the previous ones are checked.
Namely it's one of these that's going to be the problem.
So what we have to do is we have to move this statement above all the lines so that we first check if
the user choice is greater than three or less than zero.
And if that's not true, do we actually continue checking to see who won the game?
Now finally we can run our code.
Type in invalid number and get you typed in invalid number.
You lose.
So now that we've actually got our logic all sorted out, the next step is to include our images.
And to do that, we have to somehow match it up with these numbers.
We have right zero, one and two.
Now there's many, many ways that you could do this and you could solve this project.
But the easiest way is probably putting the game images into a list where we have rock as the first
one, paper as the second one, and scissors as the last one.
So we're using the fact that lists have a Order that is always going to be followed.
So we can now get hold of the rock picture by getting game images.
Zero.
We can get scissors by doing game images two and so on and so forth, and we can match those up to our
choices, the user choice and the computer choice.
So when the user chooses a number, then we're going to print from the game images.
And we're going to use our little square brackets to select the image we want from this list.
And the one that we want is going to be based on the user's choice.
Right?
Zero for rock, one for paper, two for scissors.
So we're going to put user choice as the index to pick out an image from game images.
The index can only be zero, 1 or 2.
So we should only print the image if our user picked a number that's greater or equal to zero and less
than or equal to two.
Otherwise, our program will crash before we reach our checking code down here, where we tell our user
that they've typed in an invalid number.
Now, down here for the computer choice, instead of printing the number that the computer chose, I'm
going to delete that and add a little colon.
So that way it's going to show computer chose.
And then I'm going to print the image of the choice that the computer made.
So I'm going to add another print statement.
I'm going to tap into the game images.
And then inside some square brackets I'm going to use the computer choice which is going to be between
zero, one and two as the index to pick out the corresponding image from my list.
Game images.
So now we're finally ready to run our code, and I'm going to choose zero for rock and the computer
to chose paper.
So paper beats rock, I lose.
Let's try this again.
I'm going.
To choose scissors and computer chose scissors.
It's a draw.
Now we have completed the project by using everything from lists to randomization to variables to if
statements and a whole lot of logic.
So how did you get on with this project if you struggled?
This is the time again to go back to it and maybe map things out using a flow diagram.
But the important thing is that you got to write your own code and you've got to make it work, and
this is the only way that you'll understand what's going on so that you can continue on.
And as things get harder, you'll be able to learn more because you've already understood all of the
previous knowledge.
So I hope you had fun with me today, learning and building, and hopefully I will see you tomorrow
bright and early.
So that's goodnight from me for today.