This is part two of a two part tutorial showing how to create the classic word guessing game 'hangman' using python and pycharm! The previous episode covered 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 this 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
what's going on you guys welcome back to
the channel uh today's video is gonna be
part two in building a hangman uh game
from scratch a playable uh hangman game
with python
and if you missed the first video what
we got it to was you enter a secret word
in the game and then the game stars it
out and replaces it all with secret
characters then as you guess letters if
you get them right it fills it in
and then
see grit there you go and then the the
function ends once you've guessed all
the letters in the word so uh it that
obviously wasn't super glamorous we
haven't done things like actually draw a
character or keep track of incorrect
guesses or handled
what happens when you either win or lose
at the end of the game so if you missed
that first video be sure to go and check
it out it should have come out right
before this one
but if you're just here for the concept
in this video or uh you're following
along let's get right into it
so what we've done already is we've
checked the guests to see if we need to
update our secret word but what we're
not doing is keeping track of correct or
incorrect guesses right so you don't
want someone necessarily to be able to
re-guess a letter like you but but it's
kind of hard to keep track of everything
that you've guessed already if we don't
display it to the users
so what i'm going to do is i'm going
gonna come up where we created our
variables initially
and i'm gonna create two lists i'm gonna
create two empty lists i'll call one
guesses and i will call the other one
wrong guesses and if you think about it
uh we need to keep track of all the
letters that have been guessed so we
don't
need to run our guess function over
again if somebody's already guessed a
character but the wrong guesses is
actually more important because we need
to keep track of if you've used up all
your tries or not
so okay i've created those empty lists
because then
when we are
checking the guesses and the wrong
guesses then we need to come down to our
code
new guess so no matter what when they
when they haven't solved the word yet
we'll ask for
a new guess but then what we're going to
do is check if
new
gas
is not in
guesses so this is us checking to see
if uh
if the the letter the character that was
just guessed is not in our guesses table
already and
if it isn't then we're going to
add it to the table
so
again i said this in the last video if
you're totally unfamiliar with like
basic fundamentals of python you
probably want to go back and check out
some of the tutorials on like lists and
dictionaries and creating functions
passing in variables
because i'm not going to explain
everything in detail but this append
function we're basically saying if the
new guess hasn't already been guessed
add our new guess to the list of guesses
so that's pretty straightforward
um and then okay so that's cool then
we're gonna check our secret word see if
we need to update it and then we'll say
if the new
guess
is not in
the word
that's what's going to make it a wrong
guess right
and
new guess not in
wrong guesses so we're not going to
punish the user for trying to guess a
wrong letter multiple times
um
so this is checking to see if whatever
character they just punched in is not in
the actual answer
and the new guess is not already being
guessed then at that point we are going
to
wrong guesses dot appends just like
before and we are gonna put in
new guess
okay
so uh this isn't super duper useful
if we don't display it to the users so
let's go ahead and print out
after they've made a guess we'll print
you have
guest
and then let's just print
all of the guesses
our whole guest list
and then let's also print
these
have been
wrong guesses
and then let's print
wrong guesses
so now every time they go through
to guess a new letter they're going to
be shown every guess that they've made
already whether it was right or wrong
and that'll help them make their next
guess so let's go ahead
and run this and just see if it works
the way we think we're gonna work so
let's say our secret word is password
let's guess a
cool you've guessed a these have been
wrong nice let's put q in there you've
guessed a and q these have been wrong
the secret word is this guess a letter
p
s
w
t
nice so this is pretty sweet um i mean
this is how actual hangman works right
you write down the layers that have been
guessed or not um but you may be saying
one pretty important element of hangman
is actually drawing a figure that
progressively gets more
filled in and dead as you go and we'll
actually just do kind of a fun simple
way to do that um in in a python file
here so let's go ahead and before we
check uh before we print out the guesses
and the wrong guesses let's also do a
new function
and i'll just call it
check
dead guy
um
because that's in hangman um the number
of oops
caps lock again
um in hangman the number of wrong
guesses determine how many characters
have been drawn on your dead guy
so
let's go ahead and
let's go ahead and make this function we
called it check
dead guy and we're passing in
oh we'll call it something different so
we don't get confused
while writing the function we'll call
these bad guesses
and um this is gonna it's gonna look
kind of like a messy function because we
have to put a lot of um print statements
on here because we want to look like
we're drawing like a gallows
that will hang a guy at
so let's say no matter what
every time we check the dead guy let's
print like a
underscore underscore period
and that's going to be like the
horizontal part of gallows and then
we'll print
uh space space
vertical bar so that'll look kind of
like the the
horizontal and then vertical part of
gallows
oh those got to be strings
it's awesome
okay so that should be good for the
first two um let's go ahead and put some
spaces after because i think this needs
to be five wide now we need to start
checking our bad guesses so right
everything after just drawing the
structure that holds the our little dead
guy up
um
is going to be dependent on how many
wrong guesses there have been so let's
say if length of
bad guesses
is greater than zero so that means
there's been at least one bad guess
um then we do want to print uh our
little guy's head because that's the
first character that gets drawn so we'll
say okay well if you've made at least
one bad guess let's print out a head
and then the next piece would be kind of
similar to that if
your length of bad guesses is greater
than one right then why don't we print
out
space space his neck
okay
and
no i guess this would be the same this
would be the same row we would want to
put his arms in right so uh it's not as
simple as just saying
greater than one we have to put some
additional conditions here and check if
it's one two or three because then the
second row will look a little different
so let's say if the length is exactly
two
then all we want to do is print out this
guy
um
but
if the length is three now you've got an
arm right so
now we want to print out that guy will
say it's his left arm
and then
if it's exactly
four or greater than 4 so actually i
will just say if it is greater
than 3
because that means it's 4 or up
but in that case
we want it to print out both arms so now
you can kind of see we're gonna say okay
well you've guessed one wrong guess you
get a little head you've guessed two
wrong guesses you've got a neck three
wrong guesses you get your left arm four
and up you get your right arm and then
uh now we need to handle the next row
down for the legs
so let's go ahead and say
we want to put like a torso segment in
there
and so we will
copy our if length of bad
guesses and you could like say for each
total number of guesses just create the
whole character and say like
um
well at this point we want this whole
shape to be drawn i actually think this
is a little bit better way of doing it
by like as a coder but you're allowed to
do it however you want as long as it
works
so let's say if the length of the bad
guesses is then greater than 4
we'll also print out
a torso section
um and you you don't have to worry about
these going in order
um
because it's going to go down and check
each rung uh consecutively so like
you're only going to get
one of these three and it's only going
to happen after the head has been
printed which will only happen after
these are printed
so let's just keep rocking and rolling
say if the length of the bad guesses
um is greater than five now we have to
check two things again
we have to check if the length
is exactly six because if it is you
haven't died yet and we want to print
out
your first leg
and then
i hope that's right into two spaces
maybe that should just be one
yeah we'll see when we run it
um and then what we want to do next is
if the length
uh we'll just do else
um because we're saying now the length
of the bad guesses was greater than five
but not equal to exactly six so that's
just means you're dead so space leg
space other leg
space cool
um so that should handle drawing the
dead guy as we get up into um as we get
up into uh making mistakes and stuff
um and let's go ahead and run it so
let's check it out we're getting very
close to being done with our hangman
function let's enter a secret word
called secret
guess a okay so a wasn't in there and
you see we got this little gallows with
a head let's guess another wrong one w
okay we get a torso i'm just going to be
really bad at playing q we get that arm
y
we get the other arm you we've got
another torso
uh oh
p
all right well i think that's sweet we
just drew a little dude um
and uh let's let's go ahead and handle
now the last thing you really have to
handle anytime you build a game
is success and failure conditions so
let's go ahead and put a statement down
below while the word is not equal equal
to the sequel secret word
and we'll say if wrong
if the length
of wrong guesses
equals seven
then that means you the whole guy has
been
uh
has been killed and so we'll print out
print
you ha you
lose
you didn't
get the word in
time
yeah we'll say did not because i don't
feel like messing around with
syntaxing uh additional parentheses okay
you did not get the word in time but
then we'll say else
print
nice
the word
was
and we'll say word
okay so um one thing we do need to check
then is that we only ask for new
uh new stuff when
the length of
wrong guesses
is less than seven right so that's how
we'll exit the while loop if you either
got the word correct or you've guessed
too many times
um
so let's see two main blank lines okay
um so that way you'll exit your while
loop under either of those two
conditions and depending on which one is
uh actually what's going down then
then that'll handle our exit scenario so
i think that's all we need to do let's
go ahead and run this
oh one thing i did see just last time we
were playing it um
after you enter a secret word i think
it'd be more fun if the word disappears
so that you can't see it so i am gonna
say that we should after the secret word
has been entered
i think we should for iron range let's
set up a quick little
um we'll just print out seven lines of
of nothing so that after you enter a
secret word um we're just gonna print
seven blank lines so uh you can no
longer see the secret word so let's see
if that works
um
we'll say our secret word we want to be
hufflepuff
okay so now what's kind of cool is
because of those blank words you could
still scroll up if you want to and see
the secret word but now if you're
actually playing this you could enter a
word pass it to your friend and they're
going to know how many characters it is
and start guessing from there so let's
guess a
let's guess s d
f
well we got a whole bunch of those
all right you can see our little guy
getting drawn t
y
and then we lost because we didn't get
the word in time let's run it again and
we'll just say
and let's get it really fast this time
because we're smart
quit let's get something wrong so our
guy has something to do
let's try guessing in a again because
we've already guessed an a and you can
see it doesn't make the guy any more
dead and it doesn't add another spot to
the dead uh the wrong guesses or correct
guesses
let's guess an h nice the word was
quidditch okay so uh there we go that's
in two videos that's how you build start
to finish a a pretty cool hangman
function
from scratch and it covered a lot of
really useful things like four and while
loops a lot of string manipulation
checking length of lists
and
a lot of great uh creating functions
passing in global variables passing in
local variables
and handling different conditions so
hopefully you found that useful uh it's
obviously kind of a silly game but it
covers a bunch of really useful python
concepts
and if you did or you have any questions
about what you saw here or something you
want to see next go ahead and let me
know about in the comments below and if
you found this useful i really
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