Project:

George's Python
3 min readMar 1, 2023

Guess the Word by Python

let’s make a simple game using a function that extracts data in Python. The game we’ll create is called “Guess the Word”. The player will have to guess a secret word, and for each incorrect guess, a part of a hangman figure will be drawn until the figure is complete and the player loses.

Photo by Pawel Czerwinski on Unsplash

Here’s the code:

import random

def get_word():
word_list = ["apple", "banana", "orange", "pear", "grape", "pineapple"]
word = random.choice(word_list)
return word
def play_game():
word = get_word()
word_letters = set(word)
alphabet = set("abcdefghijklmnopqrstuvwxyz")
used_letters = set()
lives = 6
while len(word_letters) > 0 and lives > 0:
print("You have", lives, "lives left and you have used these letters: ", " ".join(used_letters))
word_list = [letter if letter in used_letters else "-" for letter in word]
print("Current word: ", " ".join(word_list))
user_letter = input("Guess a letter: ").lower()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
else:
lives = lives - 1
print("Letter is not in word.")
elif user_letter in used_letters:
print("You have already used that letter. Guess another one.")
else:
print("Invalid character. Please try again.")
if lives == 0:
print("Sorry, you died. The word was", word)
else:
print("You guessed the word", word, "!!")
play_game()

Let’s go through the code step-by-step:

  1. We start by importing the random module, which we’ll use to randomly select a word from a list.
  2. Next, we define the get_word() function. This function selects a word from a list of words and returns it.
  3. The play_game() function is the main function that runs the game.
  4. We start by calling the get_word() function to select a random word from the list. We then create a set of the letters in the word and a set of the letters in the alphabet.
  5. We also create an empty set called used_letters to store the letters that the player has guessed.
  6. We set the number of lives to 6, which is the number of incorrect guesses the player can make before losing the game.
  7. We enter a while loop that runs as long as there are still letters in the word_letters set and the player still has lives left.
  8. Within the loop, we print out the number of lives the player has left and the letters they’ve already guessed.
  9. We create a list called word_list that contains either the letter in word if it has been guessed already or a "-" if it hasn't.
  10. We print out the current state of the word by joining the letters in word_list with spaces.
  11. We ask the player to guess a letter and store the letter in user_letter. We convert the letter to lowercase to simplify checking.
  12. If the letter is in the alphabet and hasn’t been guessed before, we add it to used_letters. If it's in word_letters, we remove it from the set. If it's not in word_letters, we decrement the lives variable.
  13. If the letter has already been guessed, we tell the player to guess another letter.
  14. If the letter inputted by the user is not a valid character, the code will print a message informing the user that the input is invalid and prompting them to input a valid letter. The code achieves this using an if statement with the not operator, which checks if the input letter is not a valid character by comparing it to a string of all valid characters using the in operator. If the input letter is not in the valid characters string, the code executes the block of code inside the if statement, which prints the error message and prompts the user to input a valid letter again.
  15. Here’s the code snippet that handles invalid input:
if letter not in valid_chars:
print("Invalid input. Please enter a valid letter.")
letter = get_letter()

The get_letter() function is called again to prompt the user to enter a valid letter. This process continues until the user enters a valid letter.

for the Next step Click here

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

George's Python
George's Python

Written by George's Python

I write to help how to learn Python

No responses yet

Write a response