Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Checking for a win
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, that was fun.
0:01
And now this next part we're going to right here
0:02
what we have to do is we have to test for a winner, right?
0:05
We've already gotten the rows
0:06
we've verify that they're valid
0:08
that they are among the three options
0:10
that we can do with our game.
0:12
But how do we test who's won?
0:14
Well, it turns out, the most straightforward way to do this
0:17
is actually really complicated and not very pretty.
0:21
But what we're going to do is we're going to build this
0:22
and then we're going to improve on it
0:24
and improve upon it, okay.
0:25
So let me paste out some little basic steps that say
0:29
here's how this game is played.
0:30
So if somebody plays a rock
0:32
and then the other person plays a rock, it's at tie
0:35
the other person plays a paper, they lose.
0:37
Other person plays a scissors, they win.
0:40
But let's go over here.
0:41
And first we're going to need a way to indicate
0:43
who won this round.
0:45
So we'll say winner equals, now in Python if we want to say
0:49
we don't know what this value is
0:51
it doesn't point to anything.
0:52
Is it player_1? Is it player_2?
0:54
We don't know, it's not set yet.
0:56
The way you do that, as you say, it's none.
0:58
This means remember in the Python tutor
1:01
where we had all those arrows pointing at things
1:02
basically the conceptual idea here is this point nowhere
1:06
technically there's a thing that's created
1:08
that's called none, but that's the idea.
1:09
So that it says, usually know here this goes right now.
1:12
So what we want to do, is we want to convert this bit
1:15
into some if statements.
1:17
So let's do this first one now.
1:18
Notice there's a tie, there's a tie, and there's a tie.
1:22
So scissors scissors tie, paper paper tie, rock rock tie
1:26
Though, let's go over here.
1:28
And we'll say if, oh, what is it?
1:29
Row one is the same as or equal to.
1:32
So double equals, remember, row two
1:35
then we'll print a game was a tie
1:38
or the play was tied.
1:40
All right, so now we want to have another case.
1:43
On the other hand, what are we going to do?
1:45
Well, we're going to start out by just doing basically
1:47
this test in software.
1:49
I'm going to show you something much better later.
1:51
But remember that requires more ideas
1:53
that we're going to get to.
1:54
So if I can say row one, if this happens to be rock
1:58
now be careful, that and that is not the same.
2:02
Though if we say capital R rock
2:04
that's a totally different variable value
2:06
than lowercase rock.
2:07
So we can actually even verify that a little bit better.
2:10
So we could take away some of those challenges
2:13
that the players might have.
2:14
So if they type capital rock or something
2:16
we could also go over here and say I want to change row one
2:19
let's make it whatever it is, but we want to lowercase it.
2:22
So that's a string thing.
2:24
We also might want to say, well
2:25
if they put a space accidentally and then hit Enter
2:28
that's okay too, We're just going to ignore that.
2:30
So you can say that by saying strip.
2:31
That we go to this value, and so by it we say
2:34
row one is its current value, but lowercase
2:36
and then take away all the spaces, tabs, and so on.
2:39
That'll make things a little easier.
2:40
So we don't have to worry about checking whether or not
2:43
that's a capital or lowercase R
2:45
whatever they type is always lowercase rock
2:47
because what we just did there, right.
2:49
Now if that's the case
2:51
we have more tests that we have to do
2:52
so we have to say if row two is, ah, which one, paper.
2:57
If row two is paper, player_1 is a loser
3:01
which makes player to the winner.
3:02
So we'll say winner equals player_2.
3:05
Wow, right.
3:06
Else, now we could just say else because technically
3:09
they're not going to be the same.
3:11
But let's say make it really clear.
3:14
If this is going to be scissors, if it scissors
3:18
the person whose perspective we're looking at this from
3:21
is going to be the winner.
3:22
The winner is player_1, okay?
3:25
We need to just do that over again for paper
3:28
I'm going to move this up for now.
3:29
So you all have this as a little reference.
3:33
Like so, and going to be exactly the same.
3:36
This is going to be the next one is paper.
3:39
This is rock.
3:41
It's paper and this is rock
3:42
then the winner is the person playing that row.
3:45
And still like this.
3:48
There we go.
3:49
So if I play paper, other person plays scissors, I lose.
3:52
If I play rock or if they play rock, the paper covers it
3:56
I win, right. Do one more and then we'll have it all, okay.
3:58
I told you, this is not pretty
4:00
we're going to make it awesome.
4:01
Hang in there.
4:02
But right now it's not pretty.
4:03
The last one is if I were to play scissors
4:07
and they play rock, they're going to smash the scissors
4:10
so they win. If they play paper, I'm cutting the paper
4:13
so I win, all right.
4:15
And let's just do a little print out here.
4:17
So this will be the end of the round, a game is over.
4:20
Say if, one more test here, if not winner
4:23
or something like this, winner is none
4:27
then what we can do is we'll print it was a tie, else
4:31
and whatever the winner is, takes the game, right?
4:35
Though, we've determined the name of the player.
4:37
So that won, we're just going to say they won.
4:40
This is what happens if there's a tie.
4:41
Otherwise, somebody has won, let's just say who it is.
4:44
Wow, okay, that does not look pretty, does it?
4:47
Well, we're going to play more of that
4:49
and make it much, much nicer.
4:50
But let's go ahead and play a game
4:51
and see how we're doing here.
4:52
Remember, we're going to build it up
4:54
kind of the yucky straightforward way
4:56
and then we're going to make it way nicer with all the ideas
4:58
like functions or what not that we're studying.
5:00
My name is Michael, other players Sam.
5:03
I'm going to play paper.
5:05
Let's pick some situation where Sam is going to win
5:07
and to win this round he has to play scissors.
5:10
Michael row is paper, Sam row is scissors.
5:12
The game is over and Sam takes the game, awesome.
5:15
Let's play one more round.
5:17
It's getting tiresome to already typed in my name
5:19
and the other players name, that's fine.
5:21
Let's play paper and he's going to play rock
5:24
which case I should win.
5:25
Michael row is paper, Sam row is rock.
5:27
The game is over, Michael takes the win
5:29
with a decisive throw of rock, no, of paper.
5:33
Fun, fun fun game.
5:34
Okay, so it looks like it's working.
5:36
There's a few shortcomings here.
5:38
One this is you know, Sam can always just see
5:41
what I type in and go well you know what
5:43
it's time to play some scissors or something like that.
5:47
The other one is, what if I want to play by myself?
5:49
I don't have any friends right now
5:51
that want to play Rock, Paper Scissors
5:53
though I want to play by myself
5:54
so we can make the computer do scissors.
5:56
A few basic enhancements there.
5:57
The other one, it's very common that Rock, Paper, Scissors
6:00
is done in a best of style, right?
6:04
So, first person to win three rounds
6:06
first person to win five rounds and so on.
6:08
Well, what we've written so far, is not easy to do
6:12
but we'd have to like replicate this over and over
6:15
or do some kind of loop or something.
6:17
So we're going to do a little bit of work here as well.
6:20
More to do but still, Rock, Paper Scissors is coming along.