25 Video Views·Apr 22, 2025
This is part one of a two part tutorial showing how to create the classic word guessing game 'hangman' using python and pycharm! This episode covers creation of the code, receiving user input for guesses, handling guesses that are too long, and checking to see if the guesses are correct or not. Upon a good guess where it is a letter that appears in the answer, we update the secret word to show the letters that have been guessed!
In the next video we will handle keeping track of all past guesses, handling win/lose scenarios and drawing our dead guy as he gets progressively closer to being filled out and the game being over!
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
hello welcome back to the channel and
today uh we've got kind of a fun Python
tutorial episode we're going to learn to
build the classic uh word guessing game
hangman inside python uh completely from
scratch so uh if if you aren't familiar
with some basic python Concepts we're
going to cover a lot in this video I'll
do my best to explain each thing as we
get to it but if you're totally
unfamiliar with python from the jump
it's probably wise that you check out
some of the other tutorial videos on
this channel first uh but without any
further Ado let's go ahead and get into
this so essentially hangman uh you've
probably played it is one player or
person will uh guess a word and then the
other person only gets so many chances
to guess the letters one at a time in
that word hopefully guess the secret
word before the uh little drawn stick
figure gets fully filled in and hung uh
which is when the player guessing
loses so uh it's pretty fun and let's
just jump into kind of taking a look at
one way of writing the code for it so um
I'm going to I'm going to go ahead and
just start by defining a a variable
which is going to be our initial input
where we ask the players for the secret
word so we'll say word equals input
please enter your secret
word okay and then um um th this is
going to default to string because
Python's default inputs uh always come
in as string if we wanted to be super
sure that we read it in as a string like
if for some reason someone punched in
numerical uh characters or whatever we
could do that um I'll leave it in there
you don't need the Str Str there um and
then let's
see yeah I'm going to create a variable
called secret word and some of this will
make sense once we uh run run it and
play it a little bit here but I'm
basically going to
say for I in
range oh whoops for ION range length I
got caps lock
on for ION range uh and then the length
of our word I want the secret word put a
coal in there I want my secret word to
add add one
character of just these little stars so
if you think about what we just did we
are getting an input for a word from a
player and then we are uh making a
secret word that is that same length
that is going to be just start out so
it's kind of going to look like when you
enter your password in a text input and
the letters don't show up it's just
these stars um so that's pretty cool uh
we could do do stars or we could do um
dashes it doesn't really matter I'll do
stars I think that looks good and
secretive um okay and then uh so now
we've created a word we've created a
secret word and uh let's go ahead and
kind of
make um let's start making the game
logic so we want to say while the
word is not equal to the secret word so
the way I'm picturing us playing this
game is every time you make a guess
we're going to create functions that go
through and check if each character in
word is equal to that guess you just
made and if it is then we're going to
update only that character of the secret
word to show that letter so if you put
in apple and then guess P the secret
word now is going to be star PP star
star um so I like that way of doing it
so let's go ahead and say while the word
is not equal to the secret word this is
going to kind of be every guess now
every iteration of the game we'll start
by
printing the secret oops the secret
word
is and then we will add the secret
word all right so that's pretty cool and
then let's um let's go ahead and get a
guess because this means that if we've
gotten in this far that um that the word
hasn't been solved yet so uh I'll create
a variable right here called new guess
and let's go ahead and just uh create a
function called guess so we'll say new
guess is equal to an actual uh guess
process and let's go ahead go up here
and make the guess function so what do
we want to do every time we guess um
well we want them to input one letter
right so we'll call it guest letter and
we'll say
input guess
uh
letter and that should do that and now
let's we have to check some stuff
because if someone enters like a phrase
or accidentally enters multiple
characters we don't want to let them uh
do that so we'll just say if the length
of guest
letter is greater than
one then we're going to make them enter
it again we'll say guest
letter equals input and this time we'll
we'll tell
them uh input
guess
one
letter all right so that that should
handle if they enter a phrase or
anything um you could build some while
loop that's basically saying while guest
letter is greater than one um and and
continually make them re-enter it I'm
just going to say it once and say like
it was probably user error but uh
there's a couple ways you can write
everything so else we'll go ahead and
return guest letter right because that's
that's what we want the guest to
be all right and so then now that's just
handled the the each game Loop of
getting the next guess from the user so
now we have this variable new guess
which should have uh which should have
the new um letter in it and I guess the
next thing we're going to want to do is
check the
guess yeah so let's do um secret word
now we're going to update our word and
call it check guess okay but but I think
what we should pass in here is our new
guess right whatever new character um
has just been guessed and let's create a
function for checking the guess so
Define check
guess all right and in here uh let's go
ahead and just pull in a few globals so
let's pull in global word and Global
secret word cuz we're we're going to use
those
um we're going to use those in here I
guess we could pass them through as
variables but uh we don't have to we'll
just call them call them out as Global
so that's how you reference a variable
that exists outside of a function inside
of a function um you just call the
global so okay let's say
um let's let's create an intermediate
variable that we can just use in here
and say my word equal equals secret word
um and
then for now okay yeah for I in range so
what we're going to do now is we're
going to go through each character of
our word and I just got to be length of
word
um we're going to go through each
character of our word and we are going
to check and see if the guest character
was that character so we'll say for ION
range length of
word
if word at character I is equal to the
guess um well what do we want to do if
it's equal to guess we want to update my
word um and there's unfortunately
there's really no easy way to overwrite
just one character of a string it
doesn't allow for substitution
so we can't unfortunately we can't just
say like my word at I is now equal to
guess it's going to tell you it can't do
that sort of like partial
indexing so what we have to do is um
kind of concatenate the before and after
strings so we'll say um my word from the
beginning of the word up to character
I um and then concatenate it with our
guest and then my word
again from I + one because what we've
done is we've taken the actual character
at position I and dropped it from the
string so going from the beginning to I
the way ranges work is they don't count
um they don't count the end point so
here we're saying start at my word so
again if it was Apple and we were
replacing the L and we said go from the
beginning to the fourth character it's
going to add a then P then P again but
it's not it's going to stop at four it's
not going to include four so that's why
we can write it this way instead of I
minus one um and then we'll add the
guess and then we'll add my word um from
I + one to the end of the string so
that's basically picking the secret word
apart and then um inserting our
character into the middle of it and then
putting it back
together okay so then we'll go ahead and
once that's done we're going to return
my
word and let's put some spaces in
here and that should do it for checking
so let's go ahead and just run this real
quick and see where we're at enter your
secret word well I'll say
apple okay so here you can see right
away we get um five hidden characters
let's guess a letter let's guess a
that's pretty cool the secret word is a
star star star
P all right L let's try guessing like q
w yeah you can see nothing's there e so
um and then as soon as we guess the last
letter in the word it finishes so that's
pretty awesome um we've built the the
basics of what you need to get a
functioning game right here but
obviously things like keeping track of
Lost characters and then actually
drawing the figure um if you die it it's
not in place yet and that would make
this game a lot more fun uh you were
even able ble to see the original input
of the word that the you know user puts
in the whole time so in the next uh
video in part two of this hangman
tutorial we're going to take a look at
um kind of adding a lot of that
additional functionality that's going to
really make this a fun fully useful game
but this video is getting up there in
time so um I'm going to stop this here
that's the basics of what you need to
build a Hangman game from scratch with
python hopefully you found this kind of
cool kind of useful if you have any
comments for me or uh thoughts on what
you'd like to see next feel free to let
me know in the comments below and if you
found this or anything else on the
channel useful I always appreciate a
like and a subscribe it helps the
channel out a ton and as always good
luck with your code and thanks for
watching thanks bye