Master For and While Loops in Python to Create a Hangman Game

What will we cover?

We will cover how to loop over a list. Further, we will learn the power of a While loop. Finally, we will create a Hangman Game.

Step 1: Loop over a list or sequence

Looping over a list can be done as follows (see more about lists here).

my_list = ['First', 'Second', 'Third']

for item in my_list:

This can also be done over a string, which is a sequence.

s = "Rune"

for c in s:
    print("Inside loop")
print("Outside the loop")

Where we notice the syntax of how indentation works. What is inside and outside the loop.

Step 2: While loops and how they differ

Notice that for-loops in Python loop over a predetermined length. In the above cases, the length of the list or length of the string.

If you want a loop the is dependent on something inside the loop.

What do I mean. Let’s take an example.

value = 10

while value > 0:
    value = int(input("Value? "))

As you see – this loop is dependent on input in the loop. If you type zero or negative value, it will stop.

Step 3: Description of the Hangman Game

  • The game is as follows.
  • Computer has a list of words.
  • Computer chooses a random word from the list.
  • The player gets 10 wrong guesses (10 turns).
  • The game follows this loop
    • Computer prints the word character by character replacing with underscore those not guessed yet (initial no characters has been guessed).
    • Player guesses a character.
    • If character is not in word, a turn is withdrawn
    • If no turns left, computer wins.
    • If player has guessed all characters, player wins

Step 4: Implementing the Hangman Game

Let’s try to implement the Hangman Game

import random

words = ['father', 'enterprise', 'science', 'programming', 'resistance', 'fiction', 'condition', 'reverse', 'computer', 'python']

word = random.choice(words)

turns = 10

guesses = ''

while turns > 0:
    print(f"Turns left: {turns}")
    guessed_all = True
    for c in word:
        if c in guesses:
            print(c, end=' ')
            print('_', end=' ')
            guessed_all = False
    if guessed_all:
        print("You won")
    guess = input("Guess a character: ")
    if guess in word:
        guesses += guess
        turns -= 1
    print("You lost")

Step 5: Want more?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Learn Python

Python Like a Pro?

If you’re serious about learning Python, there’s nothing better than strong commits. At your request, we have created an improved version of this popular free online course.

This version has the following benefits to enhance your learning journey.

  1.  Tracking your progress in the course.
  2.  Questionaries to ensure you understand concepts between important lessons.
  3. Downloadable Cheat Sheets for fast lookup what you learned.
  4. Direct Q&A with the instructor to help you to understand the material better.
  5. Added material for better explanations and insider knowledge.
  6. Extra videos with more explanations and stories.
  7.  Certificate at completion.

Start the change in your life and commit to doing something amazing that you have always dreamed of.

Sign up and become part of the exclusive Python Like a Pro elite.

Leave a Reply Cancel reply

Exit mobile version