Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
All right, guys, so now that we've loaded up all the knowledge that we need into our brains, it's
2
time to tackle the final project of the day.
3
And as I showed you in the beginning of the day, it's a Rock Paper Scissors game that we can play
4
with the computer.
5
So the game starts out by asking you to type 0 for rock, 1 for paper, or 2 for scissors.
6
So let's go ahead and type two for scissors, and then it's going to show you a graphic of your choice scissors,
7
and then the choice the computer made, which is rock. And of course, rock beats scissors
8
so you lose.
9
What this game boils down to is some way of randomly making a choice between rock, paper, or scissors
10
and then comparing that choice that the computer randomly generated against your choice.
11
And then based on the rules of rock, paper, and scissors, determining whether if you won, you lost or
12
whether if you had a draw.
13
While I was looking around, I actually came across the world rock paper scissors association.
14
And they have the official rules of the state of Rock Paper Scissors.
15
If you're not familiar with this game, it might be worth checking out this website.
16
Here are the three shapes of rock, paper, scissors.
17
And then the rules are that rock wins against scissors, scissors wins against paper, and paper wins
18
against rock.
19
Having all of this in mind, head over to replit/@appbrewery/ rock-paper-scissors-start
20
and go ahead and just fork the starting project.
21
Now you'll notice that in the starting project, I've already got the ASCII art for rock, paper and scissors
22
in here and they're each saved to a variable.
23
So what that means is that you can start out by just having a go at printing out let's say a rock.
24
And remember that these are variable names.
25
They're not strings, so they don't need the double quotes around them.
26
So let's run that.
27
And you can see what we get printed is the particular ASCII art that I chose in my print statement.
28
So this is already yours.
29
But as you'll notice, there's no other logic that I've provided you and you are going to rely on what
30
you learned in the previous days and most importantly, what you've learned in today's lessons to be
31
able to complete this challenge.
32
And it's worth comparing the outcome against the game of Rock Paper Scissors that I'll link to.
33
There's a couple of things to think about, namely, how are you going to decide who won or who lost?
34
How are you going to get the computer to choose a random shape, rock, paper, scissors, and how you
35
actually get this game to work in the same way that is demoed in this final version?
36
I'm going to go quiet now and I'm going to let you pause the video.
37
And I want you to spend at least ten or fifteen minutes working on this.
38
And if you get stuck, just try some things out or maybe watch some of the previous videos in the day
39
and just try to give it your best
40
go, and do a lot of struggling.
41
Hopefully you're going to succeed.
42
And once you're done, head back over here and I'll go through the solution with you.
43
So pause the video now.
44
All right, so we know that we've got all of this ASCII art, but for the moment, I'm just going to
45
start off by putting down the basic logic of this game.
46
And it's really helpful for you as well if you start thinking about breaking down the larger problem,
47
which is making the Rock Paper Scissors game into smaller problems that you can solve, like generating
48
a random number between one and three.
49
So we have some sort of proxy for Rock Paper Scissors. And then maybe thinking about putting down
50
the logic, well, if the computer chose scissors and I chose paper, then I'm going to lose.
51
Or if the computer chose rock and I chose paper, then I'm going to win.
52
And putting that down on a flowchart using draw.io or something like that can make this challenge a lot
53
easier as well.
54
But without further ado, the first thing I need to do is to produce a user choice.
55
Now, this is going to be the one that the user chose when they typed in a value.
56
And in order to get the value from them, I'm going to need to use a input.
57
And inside the input, I'm going to put the following prompt asking the user, what do you choose?
58
Type 0 for rock, 1 for paper or 2 for scissors.
59
Now, once I get hold of this input, hopefully, if the user is following my instructions, they're going
60
to type some sort of number, either zero or one or two.
61
Now that I've gotten hold of what the user wants to choose, the next thing to do is figure out what
62
the computer is going to choose.
63
So I'm going to make another variable called computer_choice and I'm going to generate a random number.
64
So to do that, remember, we have to import the random module.
65
And then we can start using it to generate random whole numbers by using random.randint.
66
And then the range is going to be between zero and two, because this is what I asked the user to do.
67
Type 0 for rock, 1 for paper, 2 for scissors.
68
So the computer is also going to choose between 0, 1 or 2.
69
Now that I've got the computer_choice, I'm actually going to just print it out for now instead of printing
70
out the actual shape, I'm just going to print out the number.
71
So computer chose and then let's go ahead and add a fstring.
72
And of course, that requires the F in front of the string.
73
So now if we just play the game as it is, remember that testing up while you're developing it is really,
74
really good practice.
75
It picks up on the bugs that you make along the way instead of waiting until the end when you've done
76
everything
77
and then it doesn't work and you have to untangle all of the lines of code.
78
At this point, I've already realized that it's actually not so great getting the user to type on this
79
line.
80
What I would much rather is to start a new line for them to type their answer on.
81
So if I run the code again, then it should look a bit like this.
82
My prompt is now here. So I'm going to choose 0 for Rock.
83
And then it tells me that computer also chose zero.
84
So in this case, this would be a draw.
85
But if I played this game again, you can see that the computer is probably going to choose something
86
different.
87
So I chose rock again
88
the computer chose 1, which is paper.
89
So now we can start thinking about how do we compare these two choices and how can we define the logic
90
of Rock Paper Scissors?
91
So I know that paper beats rock, scissors beats paper and rock will beat scissors.
92
So if zero is rock, one is paper and two is scissors, then two beats one and one beats zero.
93
So that's very simple for us to check in terms of using if statements,
94
right?
95
We could simply just check
96
well, if the computer choice is greater than the user choice, well then this probably means that the
97
computer wins.
98
But this is not true in all cases, right? Because if the computer chose 2 for scissors and the user
99
chose 0 for rock, then the user should win rather than the computer winning.
100
So we actually have some exceptions to this rule.
101
How can we catch the exceptions before we go down to this level of generalization?
102
Well, we could instead of using this as an if, we use this as an elif and we create another if statement
103
which says, well, if the user_choice is equal to zero, so if the user chose rock and the computer_choice
104
was 2, well then this means that in this case, the user actually wins.
105
Now, if we're thinking from the perspective of the user when they're playing this game, then they
106
don't really care if the computer won or the user won. They want to know, did they win or did they lose?
107
So let's change this wording a little bit.
108
We'll say, instead of user wins, we'll say you win.
109
And if the computer wins, then we'll say you lose.
110
So now if we run our program and test it out, so let's say I'm going to type 0 for rock and the
111
computer chooses 2, but we actually get an error.
112
It tells us that the greater than comparison is not supported between instances of int and string.
113
What's going on here?
114
Well, remember that we get our inputs as a number, but the input is always going to be a string.
115
So if we want this to be a number,
116
then we're going to have to wrap it inside an int.
117
So now we turn it into a whole number.
118
Now, this might be a good point to address
119
well, what should happen if they typed something that wasn't 0, 1 or 2, whether they typed 34?
120
Well, then naturally they should probably lose,
121
right?
122
So we can add an else statement addressing this situation. So we could say else we just print
123
You typed an invalid number.
124
You lose. Cool.
125
So that sorts that out.
126
Now let's run our code again and see if it works.
127
What do you choose?
128
Type 0 for Rock.
129
I'm going to choose rock again.
130
Computer chose zero and it tells me that you typed an invalid number.
131
Well, actually, zero is not an invalid number.
132
So what's going on here?
133
Well, it's because it didn't fit this criteria.
134
So I had zero,
135
computer had zero, and it didn't fit this criterion because zero is not greater than zero.
136
So it basically defaulted to the final else statement.
137
But what's actually happening here is that that was a draw.
138
Computer chose rock,
139
I chose rock.
140
That's a draw in the official rules of Rock Paper Scissors.
141
So how do I catch that?
142
Well, I could create another elif statement which says, well, if the computer_choice is equal to
143
the user_choice, well, then we're going to print
144
it's a draw.
145
Now, let's test our app again and see if there are any other situations where our code is not behaving
146
properly.
147
I'm going to choose rock again, computer chose rock as well.
148
And I guess it's a draw, so that's pretty good.
149
Now, if I choose 0 and the computer chose 2, then this is going to be the statement that gets
150
triggered and I win.
151
So that's pretty good.
152
Now, what if I chose 2 for scissors and the computer chose 0?
153
Well, in this case, rock beats scissors
154
so it should actually say I lose.
155
But instead it says you typed an invalid number.
156
So it defaulted to the else statement again because this particular situation is not caught by any of
157
these statements.
158
So what do we have to do instead?
159
Well, we actually need another elif to say, well, if the computer_choice was equal to 0 and
160
the user_choice was equal to 2, well, this means that rock beats scissors
161
so I lose and the computer wins.
162
Now, the final one that we need to catch is this case.
163
If I type 1 for paper and computer chooses 0 for rock, it again defaults to the else statements
164
whereas in fact in this case it should tell me that I won.
165
So what's missing here?
166
Well, it's the partner to this particular statement.
167
If computer_choice is greater than user_choice then I lose.
168
But on the other hand, if the user_choice was greater than the computer_choice, then I actually should
169
win.
170
So now the final thing we might have to fix is this else statement now actually never gets called because
171
this is what happens.
172
So let's say that I choose 456.
173
It tells me that computer chose 2, I win.
174
What's happening here?
175
Well, it's actually this particular line that's being carried out.
176
user_choice is greater than computer_choice.
177
This is not what we want.
178
We want it to default to
179
You typed an invalid number.
180
You lose. So we can actually just use else in this situation.
181
We actually need to provide a condition.
182
We're going to use elif to check if the user_choice is greater or equal to three,
183
now, the computer will never choose anything other than 0, 1 and 2
184
so we don't have to check that one.
185
But the user could choose something above 3 or below 0.
186
So we can use an or statement to catch this. Or if the user_choice is less than zero,
187
well, in this case, you typed an invalid number, you lose.
188
But that's not all that we have to do.
189
If we hit run and we again type some extraordinarily large number, you'll see that it still says
190
computer chose 1.
191
You win.
192
What's going on?
193
We have this statement that should catch this situation, but it's right at the bottom.
194
So it's not going to be checked until one of the previous ones are checked.
195
Namely, it's one of these that's going to be the problem.
196
So what we have to do is we have to move this statement above all the lines so that we first check if
197
the user_choice is greater than three or less than zero.
198
And if that's not true, do we actually continue checking to see who won the game.
199
Now, finally, we can run our code type an invalid number and get you typed an invalid number. You lose.
200
So now that we've actually got our logic all sorted out, the next step is to include our images.
201
And to do that, we have to somehow match it up with these numbers we have,
202
right? 0, 1 and 2.
203
Now, there's many, many ways that you could do this and you could solve this project.
204
But the easiest way is probably putting the game images into a list where we have rock as the first
205
one, paper as the second one and scissors as the last one.
206
So we're using the fact that lists have a order that is always going to be followed. So we can now get
207
hold of the rock picture by getting game_images[0].
208
We can get scissors by doing game_images[2] and so on and so forth. And we can match those up to our
209
choices, the user_choice and the computer_choice.
210
So when the user chooses a number, then we're going to print from the game_images and we're going to
211
use our little square brackets to select the image we want from this list.
212
And the one that we want is going to be based on the user's choice, right?
213
0 for rock, 1 for paper, 2 for scissors.
214
So we're going to put user_choice as the index to pick out an image from game_images. Now down here for
215
the computer_choice, instead of printing the number that the computer chose, I'm going to delete that
216
and add a little colon.
217
So that way it's going to show computer chose
218
and then I'm going to print the image of the choice that the computer made.
219
So I'm going to add another print statement,
220
I'm going to tap into the game_images and then inside some square brackets, I'm going to use the computer
221
choice which is going to be between 0, 1 and 2 as the index to pick out the corresponding image
222
from my list game_images.
223
So now we're finally ready to run our code and I'm going to choose 0 for rock and the computer chose
224
paper.
225
So paper beats rock, I lose.
226
Let's try this again.
227
I'm going to choose scissors and computer chose scissors.
228
It's a draw. But we have also introduced a bug.
229
So if we test this using a number that's outside of this range, 0, 1 or 2,
230
so let's try 5. And we hit enter,
231
what do we get?
232
We get a index error.
233
So what has happened here?
234
Well, this is up to you to debug.
235
Well, this is a really good opportunity for you to practice your debugging.
236
So think about what has
237
changed since the last time we tested this and how you can fix the code so that it works as expected,
238
so telling us that you typed an invalid number, you lose whenever we type a number that's above or
239
equal to three or less than zero.
240
So I want you to pause the video and see if you can debug this challenge so that when you type in 5,
241
you get this printed. Pause video now.
242
All right, so what's going on here, because we tested this and it worked, but in between that time,
243
we also added this line because we wanted to get a hold of the game images based on the user's choice.
244
Now, at this point, the user's choice is unchecked by the if statement.
245
So it could still be equal to 4 or 5 or 10 or a million.
246
So if in that case, it tries to get hold of the fifth image from the game_images list, well, it doesn't
247
exist.
248
There's only 0, 1 and 2.
249
So to fix this, we need to move this if statement so that it gets checked before this line of code
250
gets carried out.
251
So let's go ahead and cut this line of code out and put it right above this print statement.
252
So in this case, if the user types in a choice that's above or equal to three or less than zero,
253
we're going to print this.
254
But else, if they typed in any other number, then we want the rest of the code to continue.
255
So let's select the rest of the code, hit tab and tab it over
256
so that is included under the else statement.
257
Now let's go ahead and change this from elif just to an if because we're no longer checking it as another
258
else-
259
if statement. It is just a straight up
260
if. So, in this case, if they typed a 5, then it will go into this basket and it will finish there.
261
But otherwise, it will go into this basket and it will continue checking later on.
262
So did you manage to debug your code and figure out the issue?
263
This is one of the most important skills
264
as a programmer. You have plenty of opportunities coming up to practice your debugging.
265
And if you ever want more practice, head over into the Q&A and see what other students have issues
266
with and see if you can help them out by practicing your debugging skills.
267
Now, we have completed the project by using everything from lists to randomization to variables to
268
if statements and a whole lot of logic.
269
So how did you get on with this project? If you struggle
270
this is the time again to go back to it and maybe map things out using a flow diagram or try to run
271
the code using Thonny to see how it does it step by step.
272
But the important thing is that you got to write your own code and you got to make it work.
273
And this is the only way that you'll understand what's going on so that you can continue on.
274
And as things get harder, you'll be able to learn more because you've already understood all of the
275
previous knowledge.
276
So I hope you had fun with me today
277
learning and building, and hopefully I will see you tomorrow bright and early.
278
So that's good night from me for today.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.