Coding

How to program Rock, Paper, Scissors in Python!

  • 00:00:00 what's up guys and welcome back to
  • 00:00:01 another video in today's video we're
  • 00:00:03 gonna walk through programming a
  • 00:00:05 rock-paper-scissors shoot game in Python
  • 00:00:07 by the end of the video you'll have
  • 00:00:09 something that looks like this so
  • 00:00:13 basically you just type in a gas the
  • 00:00:16 computer makes a random gas and then the
  • 00:00:19 result is printed out and then the way I
  • 00:00:21 set this up is you can keep typing in
  • 00:00:23 guesses so paper you guess paper Xperia
  • 00:00:27 scissors sorry you lose damnit paper
  • 00:00:29 we tied let's see if I can win one rock
  • 00:00:33 rock come on do me solid
  • 00:00:34 yeah I win and one thing to note too is
  • 00:00:37 like if you typed in the rock like
  • 00:00:38 something that wasn't valid like this it
  • 00:00:42 would also tell you invalid so when
  • 00:00:44 print out a result for that this is a
  • 00:00:46 good exercise to get more comfortable
  • 00:00:48 with your programming skills and I'm
  • 00:00:50 gonna add a little bit of a spin onto
  • 00:00:51 this so after we finished this Rock
  • 00:00:53 Paper Scissors shoot game kind of the
  • 00:00:55 traditional way to program it I'm gonna
  • 00:00:57 make another video that's kind of a
  • 00:00:58 challenge problem where you take the
  • 00:01:00 same game and you write it without any
  • 00:01:03 if statement so that's like where we're
  • 00:01:06 headed and I'm gonna go through like
  • 00:01:07 design decisions a bit about programming
  • 00:01:10 different like programming the same game
  • 00:01:13 in multiple different ways their
  • 00:01:14 programming the same thing in multiple
  • 00:01:16 different ways and the trade-offs of
  • 00:01:17 doing both so let's just get into it
  • 00:01:20 okay to start off open up a new Python
  • 00:01:22 file doesn't really matter what you name
  • 00:01:25 it but make sure to include that dot py
  • 00:01:27 file extension I'll be programming in
  • 00:01:29 Python 3 using sublime text 3 is my
  • 00:01:32 editor but you can use whatever editor
  • 00:01:33 you prefer so I always think it's a
  • 00:01:35 little bit difficult like going from
  • 00:01:37 this blank slate we have right here to
  • 00:01:39 actually like producing my first couple
  • 00:01:41 lines of code so I think it becomes a
  • 00:01:43 little bit easier if you give yourselves
  • 00:01:44 a like a first simple task to do and I
  • 00:01:47 think a good first task is to write a
  • 00:01:50 couple lines of code that takes in your
  • 00:01:53 input so rock paper or scissors and
  • 00:01:55 stores it into a variable let's say it's
  • 00:01:57 called choice so we have choice equals
  • 00:02:00 and we want this to be equal to whatever
  • 00:02:02 we input and luckily for us there's a
  • 00:02:04 nice easy built-in function in Python 3
  • 00:02:06 called input and that will wait for us
  • 00:02:09 to just type something in and then store
  • 00:02:11 the result in choice
  • 00:02:13 one thing that's a little bit annoying
  • 00:02:14 about sublime text is by default it
  • 00:02:17 doesn't work with this input so I can't
  • 00:02:19 just type in the console down here rock
  • 00:02:22 let's say and have it actually store
  • 00:02:23 that in choice so there's a couple
  • 00:02:25 workarounds for this the first is to
  • 00:02:28 install this sublime repl and I'll link
  • 00:02:31 to this in the description basically I
  • 00:02:33 can run the same file through this and
  • 00:02:36 now if I type down rock it will take in
  • 00:02:40 rock stored in choice and then you know
  • 00:02:43 end the program and we can see that it
  • 00:02:45 actually took in my choice if I go ahead
  • 00:02:47 and say my choice is then I print out
  • 00:02:53 choice as well if I run this again rock
  • 00:02:59 my choice is rock so I got it now so one
  • 00:03:03 thing you can do if you're using sublime
  • 00:03:04 text 3 is to install sublime repl
  • 00:03:07 another thing you could do is just go
  • 00:03:09 ahead and actually run it from idle
  • 00:03:11 which is the default editor that Python
  • 00:03:14 comes with then the final thing you
  • 00:03:16 could do is if you're familiar with the
  • 00:03:18 commit command line you could CD into
  • 00:03:22 the directory so CD means state change
  • 00:03:24 directory into the directory where your
  • 00:03:27 code is stored so for me it's in this
  • 00:03:29 YouTube folder then it's in this folder
  • 00:03:31 called actual code and then finally it's
  • 00:03:33 in this folder called RPS and the nice
  • 00:03:35 thing too when I'm doing this in the
  • 00:03:37 command line is I hit tab it
  • 00:03:39 autocompletes for me but now I can run
  • 00:03:41 Python RPS py and now if I type in rock
  • 00:03:46 here I can also write it here so these
  • 00:03:48 are a couple different options you could
  • 00:03:49 do if the default in sublime text
  • 00:03:52 doesn't work for you ok now that we have
  • 00:03:56 our guests stored in a variable I think
  • 00:03:58 the logical next step is let's Auto
  • 00:04:00 generate a guess for the or a choice for
  • 00:04:05 the computer so we'll say all the
  • 00:04:08 possible choices and rock-paper-scissors
  • 00:04:09 are we're gonna make this a list you
  • 00:04:11 have Rock obviously you have paper and
  • 00:04:14 you have scissors so we want the
  • 00:04:18 computer so computer choice to be one of
  • 00:04:24 these three things and it wouldn't be
  • 00:04:26 good for me to just go
  • 00:04:27 and say something like choices 0 because
  • 00:04:29 then no matter how many times I run this
  • 00:04:31 program we're always gonna choose rock
  • 00:04:33 so ideally we want it to choose this
  • 00:04:35 randomly so in order to do this we want
  • 00:04:39 to import the random library of Python
  • 00:04:41 and there's now a couple different ways
  • 00:04:44 I could do this the first way is I could
  • 00:04:47 use this random library and use a
  • 00:04:49 function called randant
  • 00:04:50 in index a random position and choices
  • 00:04:53 to get my computer choice so random dot
  • 00:04:56 R and N and I want it to be between 0
  • 00:04:59 which will be this first index and then
  • 00:05:02 the length of choices so I'm gonna say 0
  • 00:05:05 comma length choices that will give me
  • 00:05:08 the correct positioning and I can go
  • 00:05:11 ahead and do print and computer choice
  • 00:05:13 now and I'll make it the same format and
  • 00:05:18 say computer choice is common computer
  • 00:05:22 choice so let's run this again rock and
  • 00:05:31 what happens if I run this one more time
  • 00:05:33 I want to just make sure that we get
  • 00:05:35 some different options Rock what the
  • 00:05:40 heck I don't know if this is is my
  • 00:05:43 problem or if I'm just getting unlucky
  • 00:05:45 come on one more
  • 00:05:48 Python one current file rock
  • 00:05:52 yeah computer choices scissor so I did
  • 00:05:54 do it randomly this time we kind of
  • 00:05:55 validate that and one thing I guess
  • 00:05:58 temporarily I want to just comment this
  • 00:06:00 out just make my life easier and just
  • 00:06:03 say my choice is equal to rock just for
  • 00:06:06 the time being I run that what happened
  • 00:06:13 yeah we got some error here so maybe
  • 00:06:15 things weren't working perfectly random
  • 00:06:18 random length choices and I guess this
  • 00:06:20 is inclusive
  • 00:06:21 so I'm guessing choices was length
  • 00:06:24 choices is 3 so it gave me 3 back as one
  • 00:06:27 of the choices so index outside of this
  • 00:06:29 so what I actually have to do is length
  • 00:06:31 choices minus 1 then I should be able to
  • 00:06:33 run this and you should see as you can
  • 00:06:37 see down below that keeps changing every
  • 00:06:39 time I rerun it
  • 00:06:40 every time it flashers on rerunning it
  • 00:06:44 sometimes you know you get the same
  • 00:06:46 thing in a row but it is pretty random
  • 00:06:49 so that's good to see alright now that
  • 00:06:51 we have both our choice and a computer
  • 00:06:54 choice we should start comparing the do
  • 00:06:56 and like printing out who is the winner
  • 00:06:58 so I'm gonna say we can start off with
  • 00:07:03 if our choice equals equals rock and
  • 00:07:07 then let's say if the computer choice
  • 00:07:12 choice
  • 00:07:13 equals equals rock then print it is a
  • 00:07:18 tie and we can continue this I can do an
  • 00:07:22 elf's if computer choice equals equals
  • 00:07:26 paper I like doing it in order then so
  • 00:07:31 if we have rock they have paper then
  • 00:07:34 they beat us with paper which I honestly
  • 00:07:36 like the rules of Rock Paper Scissors
  • 00:07:39 shoot if you really think about it why
  • 00:07:40 does paper beat Rock I don't know but
  • 00:07:43 that's how it is so print you lose sorry
  • 00:07:50 computer wins in that case and then
  • 00:07:52 finally LF computer choice equals equals
  • 00:07:56 scissors we want to print you win
  • 00:08:03 Congrats smiley face all right so let's
  • 00:08:08 test to see if that works so I'm gonna
  • 00:08:10 run it my choice is raw computer choices
  • 00:08:14 rock it is a tie my choice is raw
  • 00:08:17 computer choices paper you lose sorry
  • 00:08:20 and then finally one more time hopefully
  • 00:08:22 we get scissors eventually scissors you
  • 00:08:24 and congrats awesome and also just note
  • 00:08:27 I'm still just hard coding this in just
  • 00:08:28 because of the whole sublime text issue
  • 00:08:31 with input it just makes it easier to
  • 00:08:33 test these things quickly okay now that
  • 00:08:37 we have the rock-paper-scissors cases
  • 00:08:39 working for our choice being rock let's
  • 00:08:42 duplicate this and make it work for
  • 00:08:44 paper and scissors as well and if you're
  • 00:08:46 thinking right now Keith bro like you
  • 00:08:48 could do this a lot more efficiently
  • 00:08:50 like an optimize this I do know that and
  • 00:08:53 we will get to that
  • 00:08:54 will optimize and clean our coat after
  • 00:08:56 we just have our like first working
  • 00:08:58 version so I'm going to just copy all
  • 00:08:59 these lines and paste them so now if
  • 00:09:02 choice equals equals paper so I want to
  • 00:09:07 just make it easy on myself and just
  • 00:09:09 change the order of these as well
  • 00:09:10 computer choice equals paper it is a tie
  • 00:09:13 right
  • 00:09:13 if computer choice equals scissors then
  • 00:09:17 we would lose and if computer choice
  • 00:09:19 equals equals rock then we had win now I
  • 00:09:23 could go ahead and change this to paper
  • 00:09:25 and just check make sure all these are
  • 00:09:27 working my choice of paper computer
  • 00:09:29 choice is paper it is a tie my choice is
  • 00:09:31 paper computer choice scissors you lose
  • 00:09:34 sorry etc and one more time we can do
  • 00:09:37 this again oh no what did I do
  • 00:09:40 we can do this again for the last option
  • 00:09:44 we have of choosing and that is scissors
  • 00:09:46 so I can go ahead and do choice equals
  • 00:09:50 scissors and in that case this would be
  • 00:09:54 a tie situation B scissors the loot you
  • 00:10:01 lose case would be rock and that you win
  • 00:10:06 case would be paper and we'll put all
  • 00:10:11 this together and instead of hard-coding
  • 00:10:12 this right now I'm going to comment this
  • 00:10:14 out and will actually accept our input
  • 00:10:17 so choice equals input we're going to
  • 00:10:19 run this with sublime repl just so we
  • 00:10:21 can actually get that input run current
  • 00:10:24 file and I'll type in rock my choice is
  • 00:10:28 rock computer choices paper let's see if
  • 00:10:30 we can run this again you know I'll do
  • 00:10:36 paper this time my choice is paper
  • 00:10:39 computer choices paper it is tie that
  • 00:10:41 looks good and then one last sanity
  • 00:10:44 check let's just see if something this
  • 00:10:47 is not what the heck did I do this is
  • 00:10:50 not fully testing everything but I just
  • 00:10:53 want to make sure that it accepts
  • 00:10:54 scissors and does something off of that
  • 00:10:56 so scissors my choice is scissors
  • 00:11:00 creative choices paper you and Congrats
  • 00:11:03 smiley face cool and one thing that I'm
  • 00:11:07 seeing
  • 00:11:08 right now as I do this it's like kind of
  • 00:11:10 weird when I don't have any sort of text
  • 00:11:12 to let me know I should make an input so
  • 00:11:15 make your choice so I'll do something
  • 00:11:19 like this now if I rerun this current
  • 00:11:26 file make your choice now it's a little
  • 00:11:27 bit more obvious to me that okay I can
  • 00:11:29 type something in now rock alright next
  • 00:11:33 because this is getting kind of annoying
  • 00:11:34 that I keep having to run like a new one
  • 00:11:36 of these windows every time we're gonna
  • 00:11:39 surround our code with a while true so
  • 00:11:42 basically instead of just running once
  • 00:11:44 if we have a while true around this it
  • 00:11:48 will just continually ask us for a new
  • 00:11:50 input so if I do this while true then
  • 00:11:53 all the code I used to have I need to go
  • 00:11:55 ahead and indent that to signify that it
  • 00:11:58 is inside of this while true so if I
  • 00:12:01 rerun this make your choice rock
  • 00:12:07 make your choice paper and so now I can
  • 00:12:11 more efficiently test this and one thing
  • 00:12:14 I think it would be nice is let's at the
  • 00:12:17 end of our code before we get to the
  • 00:12:19 next iteration of the loop let's print
  • 00:12:22 out just an empty line just to give
  • 00:12:24 ourselves a little bit of separation run
  • 00:12:28 this again make your choice rock un yay
  • 00:12:34 scissors you lose no and alright so we
  • 00:12:41 have a game that's like fully working
  • 00:12:43 right now but we can make this better so
  • 00:12:45 we can make this better in a couple ways
  • 00:12:46 I guess functionality we can make this
  • 00:12:49 better and we also can clean our code
  • 00:12:50 one thing that we can prove with the
  • 00:12:52 functionality is let's say I put rock
  • 00:12:54 and all caps
  • 00:12:58 it doesn't really know what to do with
  • 00:13:00 this since my choice is raw computer
  • 00:13:03 choices Rock but it didn't tell us it is
  • 00:13:04 a tie because in our code we have choice
  • 00:13:10 equals equals rock and all lowercase so
  • 00:13:13 to fix this issue one thing we can do is
  • 00:13:16 after we get our choice we can do choice
  • 00:13:18 equals choice dot lower and that will
  • 00:13:21 take the string string that we got from
  • 00:13:23 this input and just make it all
  • 00:13:25 lowercase so I go ahead and run this now
  • 00:13:36 if I do rock like this you'll see now it
  • 00:13:40 lowercase it when I printed it out and
  • 00:13:42 now it like could validly check a I
  • 00:13:45 guess end game case taking this a step
  • 00:13:50 farther we probably want a way to tell
  • 00:13:53 the user if they type in something that
  • 00:13:55 isn't rock paper or scissors that they
  • 00:13:57 have an invalid guess so that will be
  • 00:14:00 the next thing we do so the way we can
  • 00:14:03 do this is we could add another if
  • 00:14:05 statement outside of all of these
  • 00:14:06 choices and I'm going to add a statement
  • 00:14:12 that just says if choice in choices so
  • 00:14:17 basically it's saying you know if the
  • 00:14:19 choice that we made right up here
  • 00:14:21 actually is exists in this list then
  • 00:14:25 it's valid and we can like go to one of
  • 00:14:27 these cases otherwise what we'll want to
  • 00:14:30 do is else print like invalid choice try
  • 00:14:38 again so let's see if this works I'm
  • 00:14:41 gonna close out these tools this is an
  • 00:14:46 only thing to do this run current file
  • 00:14:49 make your choice rock that works right
  • 00:14:53 now if I say yeah and I run that my
  • 00:14:59 choice is the computer choices scissors
  • 00:15:02 valid choice try again so now I can try
  • 00:15:04 rock again and as you can see it starts
  • 00:15:07 working again cool so we fixed that
  • 00:15:09 little issue the next thing we'll go
  • 00:15:11 ahead and do is if you look at all of
  • 00:15:13 our code one sec if you look at all of
  • 00:15:17 our code here like in all these if an
  • 00:15:19 elephant's statements this definitely
  • 00:15:22 could be cleaned up there's there's code
  • 00:15:24 that's unnecessary here and I think it
  • 00:15:28 would be good for us to try to make this
  • 00:15:31 a little bit neater and one thing also
  • 00:15:32 to think about is my next video is going
  • 00:15:34 to be on
  • 00:15:35 this game with out if an LF and else so
  • 00:15:39 like try to think about starts thinking
  • 00:15:42 about like how the heck could you do
  • 00:15:43 this we need if an elf
  • 00:15:45 an if an LF and else so often in this
  • 00:15:48 this curtain version but okay let's
  • 00:15:50 start optimizing and the first thing
  • 00:15:52 that I see we do is we always if there's
  • 00:15:57 computer choices equal to our choice
  • 00:15:59 right here it is at I we can simplify
  • 00:16:03 this so we don't even actually need any
  • 00:16:05 of these lines so I'm going to delete
  • 00:16:08 all of these it is a tie lines so I just
  • 00:16:12 deleted three if statements with like a
  • 00:16:15 message inside and what we can do
  • 00:16:17 instead is we can just say if choice
  • 00:16:20 equals equals computer choice and that
  • 00:16:23 will encompass all three of those
  • 00:16:24 examples I can now print it is a tie
  • 00:16:32 cool tools and we'll build that just
  • 00:16:37 check make sure that it works Python run
  • 00:16:40 can file oh no what did I do
  • 00:16:43 l if computer choice
  • 00:16:46 oh now because I deleted these if
  • 00:16:51 statements this doesn't become an L if
  • 00:16:54 this is still these all have to be ifs
  • 00:16:56 tools Rock I'm gonna just keep going
  • 00:17:06 until I get a tie yep still works that's
  • 00:17:08 awesome and let's see if it worked with
  • 00:17:10 scissors as well scissors if I can get
  • 00:17:15 another scissors yep still works with
  • 00:17:18 all of them so that's cool that's one
  • 00:17:19 way we can simplify it other ways we
  • 00:17:22 could simplify it is I guess we can be
  • 00:17:25 because we should we always will get a
  • 00:17:28 valid I guess choice here as the
  • 00:17:31 computer player actually one thing I can
  • 00:17:34 do here is instead of doing the indexing
  • 00:17:37 solution there's another way we can
  • 00:17:39 actually do this there's another
  • 00:17:40 function in random if you look at the
  • 00:17:42 random library documentation you'll find
  • 00:17:45 this but you can actually do random
  • 00:17:46 choice choices and then we don't even
  • 00:17:49 actually have to
  • 00:17:49 index so this would also work for the
  • 00:17:52 computer choice just another way to
  • 00:17:53 write that maybe a little bit neater
  • 00:17:55 maybe not I guess it's kind of up to you
  • 00:17:58 but I guess something that I can do here
  • 00:18:01 is and this is also kind of I would say
  • 00:18:04 up to you is instead of doing LF I could
  • 00:18:07 just do else here because I know that
  • 00:18:08 this is the only other case because
  • 00:18:11 we've already taken care of the tie case
  • 00:18:13 so it's either paper or the other case
  • 00:18:16 would be in this case would be scissors
  • 00:18:21 and that can be just encompassed by an
  • 00:18:25 else so I'm going to just do that for
  • 00:18:27 all of them some people might say that
  • 00:18:36 it's better to do it actually be
  • 00:18:38 explicit like this just because if
  • 00:18:39 there's some sort of weird bug that
  • 00:18:40 comes in but I'll let you guys make that
  • 00:18:43 decision so now we we cleaned up the
  • 00:18:46 code a bit I would say okay so at this
  • 00:18:50 point I would say that we have our fully
  • 00:18:51 functioning a rock-paper-scissors shoot
  • 00:18:53 game if you have any questions about
  • 00:18:57 anything that I did in this video let me
  • 00:18:59 know in the comments if you liked the
  • 00:19:01 video make sure to throw it a thumbs up
  • 00:19:02 and also make sure to subscribe and
  • 00:19:05 don't forget to check out the next video
  • 00:19:08 I'm posting which is going to be the
  • 00:19:09 same exact game but in this next time
  • 00:19:12 instead of using all these if statements
  • 00:19:14 will be doing using absolutely zero if
  • 00:19:17 statements in our code so it's kind of
  • 00:19:19 an interesting problem like being able
  • 00:19:21 to design a game in a completely like
  • 00:19:23 different way and there's definitely
  • 00:19:24 trade-offs and I guess perks of both
  • 00:19:27 implementation so make sure to check
  • 00:19:29 that out
  • 00:19:30 I think that's all I have thanks again
  • 00:19:33 for watching guys and peace out
  • 00:19:37 [Music]