Project:
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.
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:
- We start by importing the random module, which we’ll use to randomly select a word from a list.
- Next, we define the
get_word()
function. This function selects a word from a list of words and returns it. - The
play_game()
function is the main function that runs the game. - 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. - We also create an empty set called
used_letters
to store the letters that the player has guessed. - We set the number of lives to 6, which is the number of incorrect guesses the player can make before losing the game.
- 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. - Within the loop, we print out the number of lives the player has left and the letters they’ve already guessed.
- We create a list called
word_list
that contains either the letter inword
if it has been guessed already or a "-" if it hasn't. - We print out the current state of the word by joining the letters in
word_list
with spaces. - We ask the player to guess a letter and store the letter in
user_letter
. We convert the letter to lowercase to simplify checking. - If the letter is in the alphabet and hasn’t been guessed before, we add it to
used_letters
. If it's inword_letters
, we remove it from the set. If it's not inword_letters
, we decrement thelives
variable. - If the letter has already been guessed, we tell the player to guess another letter.
- 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 thenot
operator, which checks if the input letter is not a valid character by comparing it to a string of all valid characters using thein
operator. If the input letter is not in the valid characters string, the code executes the block of code inside theif
statement, which prints the error message and prompts the user to input a valid letter again. - 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