Close this search box.

Making a simple hangman game in Python

Today I thought it might be cool to make a super simple little text based game in Python in my spare 15 minutes. So I made this hangman game.

As you can see, the word chosen is ‘squiggle’. As the user selects new letters, all occurences of those letters are uncovered in the list. If you were really going to make this into a game, you might have a list of 100 words, which you would choose at random, so the user didn’t see the word. But this, is really to walk through the logic and how I implemented it.

So, we take the users input and create a list to store our correct guesses. I now fill the list with asterisks. So if the word has 4 characters, the list will be [*, *, *, *].

As the user goes through the game, they have a possible 10 wrong answers, after that point, they lose.

With each guess, I loop through the word and replace the * in the correct_guesses list with the letter that the user correctly guessed, once there are no stars left in the list, there are no more letters to guess & hence the user wins.

Here is the code:

word = input('pick a word ')
correct_guesses = []
for letter in word:
losses = 0
status = 'playing'
while losses < 10 and status == 'playing':
  i = 0
  guess = input('guess ')
  #### is the letter in the word - if not, increment failure count
  if guess in word:
    print('We found it!')
    while i < len(word):
      if guess == word[i]:
        correct_guesses[i] = guess
        if '*' not in correct_guesses:
          print('you win')
          status = 'won'
      i = i + 1
    losses = losses + 1
    if losses == 10:
      status = 'lost'
      print('You lost')
      print('its not there')
Share the Post:

Related Posts