Code a rock paper scissors game to play against computer AI with python! Great beginner project pt.2

LeMaster Tech
182 followers
13 Video Views·Apr 22, 2025
This is part two of a two part tutorial showing how to create the classic guessing game 'rock paper scissors'! In this video we will handle building a game that can be rapidly replayed and reused as well as keeping track of total score!
In the previous video we get an input from the player to determine their guess, and then randomly generate an 'AI' computer player's guess and determine who wins or loses by comparing using nested if loops in basic functions!
If you like the content from this page please like and subscribe to the channel to see lots more like this! Thanks so much for supporting the page and good luck coding!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
hey what's going on you guys welcome
back to the channel and welcome to part
two of our uh creating a rock paper
scissors game from scratch with python
and if you missed part one i recommend
you go and check that out essentially
what we created was a format where you
pick rp or s and then the computer picks
rp or s just so you don't have to type
in rock paper scissors every time and
the computer's pick is randomly
generated using the random integer
function
and then we process whether that would
be a win loss or draw based on what you
and the computer have picked
so today we're going to look at some
things that just make it a more fun game
a more reusable code and this isn't just
specific to rock paper scissors this is
kind of a good practice thing to do for
python
so what we're going to do is we're going
to create a game loop and basically
anytime you're creating a
reusable code you're going to want to do
something along these lines so we'll say
for this
while play again
does not equal
no
um and
while play again does not equal no we'll
run rock paper scissors but then what we
need to do is we need to actually get
the status of that play again variable
from rock paper scissors so that we
don't have an infinite game loop
um but then let's go ahead and
do
also pass in
this actually we don't need to pass it
in we're going to get that back from
them
so uh what we want to do every time the
game is finished so draw loss or win is
we then want to create this like play
again
prompt and we want it to be an input
where we ask the user
would you uh let's just keep it short
and say play again question mark and
then let's tell them we would like to
get back either yes or no okay and let's
put that
in uh all three scenarios
and then let's go ahead and return once
the loop is once the if statement has
executed let's go ahead and return the
play again prompt
and so
let's just run that and see if it lets
us keep playing
and
no is not defined
oh play again is not defined right yeah
um so we'll just come up here in the
beginning
and we will
create an empty string called play again
so the initialize variable let's go and
say rock we lost but we want to play
again and let's do paper and we won and
do we want to play again no i won okay
so that exits the code that's cool um we
just created replayability but then what
else would be kind of nice in this
function is if we also added some score
tracking so there are basically three
outcomes that could happen we could have
wins
um
losses
and draws or ties
and so we'll just start out by making
all three of those zeros but then every
time we call this function to have those
updated we have to pass in win loss and
draw or we would have to make them
global variables
and then to update them in the outside
world we're also going to have to make
sure that we're updating them out here
so let's go ahead and
create them
and
losses draws create them everywhere and
pay special attention to order because
this does matter
so for our
rock paper scissors game now we are
going to be passing in wins losses and
draws and what's kind of nice is we
already wrote the code in part one for
tracking the scenario under which you
would win or you would lose
or tie so we'll go ahead and now we can
just easily update the variables to add
one to themselves every time something
happens so
here's losses
add one to it
and then here's wins
add one to it so it's basically
incrementing every time that scenario
happens and then the last piece to make
this really useful is going to be
[Music]
print a score prompt so let's go ahead
and print
and we'll say
winds colon space and then we'll add the
variable winds but that's going to be an
integer so make sure you add the str
function to make it a string
and then we'll add another one and we'll
start with a space and say
losses colon space
and then let's add the string version of
losses
and let's just do it one more time for
draws and then we'll do
we'll do the string
of the variable draws
and i'm just going to copy this exactly
copy this
and put it in all three places
so hopefully every single iteration of
this game loop as the score increments
um we also see win-loss draw
um going up
draw is not defined because it needs to
be blur alrighty let's try running that
again
all right let's pick rock
and we tied okay but then we can see the
score up updating so we picked paper oh
i'm supposed to enter yes
okay and now we pick paper
and we won
and let's play again
and let's pick rock
and let's play again
and let's pick scissors
okay well we can see our score and our
losses uh counting up and counting up
now let's say no we're done all right
and we've exited um one thing i like to
add to most of my functions especially
for making a game or something that was
supposed to be fun for the user i like
to print a statement on the way out that
just says like
thanks for playing or something like
that you know um and so this way after
you play rock okay i won cool now i want
to leave no you just leave him with a
nice little message
so
this is pretty cool we just built a
really fun replayable rock paper
scissors against a computer like ai
in just a few minutes and hopefully
found this useful if you have any
questions about this or anything you'd
like to see next be sure to let me know
about in the comments below i'll get
back to you as soon as possible and if
you found this or anything else on the
channel useful i really appreciate a
liking to subscribe it helps the channel
out a ton as i'm getting started here
and as always good luck with your code
and thanks for watching thanks bye
