0% found this document useful (0 votes)
4 views9 pages

Phthon Final Project Explanation

The document explains a text-based Hangman game project in Python, detailing its mechanics and code structure. The game allows players to guess a hidden word by entering letters, with a maximum of six incorrect guesses before losing. It includes a step-by-step code explanation, covering class definition, game logic, and user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Phthon Final Project Explanation

The document explains a text-based Hangman game project in Python, detailing its mechanics and code structure. The game allows players to guess a hidden word by entering letters, with a maximum of six incorrect guesses before losing. It includes a step-by-step code explanation, covering class definition, game logic, and user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PHTHON FINAL PROJECT EXPLANATION &

CODE STEP-BY-STEP

PROJECT EXPLANATION:
🏆 Project Title

Hangman Game (Text-based Python Project)

🎮 Project Overview

This is a text-based Hangman game written using basic Python concepts.


The game randomly picks a hidden word, and the player has to guess it by
entering one letter at a time.

✅ If the letter is in the word → it’s revealed.


❌ If the letter is wrong → player loses one life.

The player wins if they reveal the full word before running out of lives (6 chances).

⚙ Working

1️⃣ Game picks a random word from a list.


2️⃣ Player sees the word as underscores (_ _ _ _ _).
3️⃣ Player inputs guesses (one letter at a time).
4️⃣ The game updates the display or decreases lives based on correctness.
5️⃣ The game ends when:

 All letters are guessed → win 🎉


 Lives reach zero → game over 💀

🛠 Step-by-step Code Explanation


1. Import the random module

import random

We use Python’s random module to randomly select a word from the list.

2️⃣ Define the HangmanGame class


class HangmanGame:

We create a class to organize all game logic, using OOP (Object-Oriented


Programming) concepts

3️⃣ Constructor (__init__ method)

def __init__(self, word_list, lives=6):

self.word = random.choice(word_list).lower()

self.lives = lives

self.guessed_letters = []

self.display_word = ['_' for _ in self.word]

✅ self.word → picks a random word.


✅ self.lives → sets starting lives (default 6).
✅ self.guessed_letters → keeps track of letters the player guessed.
✅ self.display_word → shows the word with underscores initially.

4️⃣ Show game progress

def show_progress(self):

print('Word: ' + ' '.join(self.display_word))

print(f"Lives left: {self.lives}")

print(f"Guessed letters: {',


'.join(self.guessed_letters)}\n")

This method prints:

 The current word status (_ y _ h o n)


 Remaining lives
 Letters the player has already guessed

5️⃣ Process player’s guess

def guess_letter(self, letter):

if letter in self.guessed_letters:

print("You already guessed that letter.\n")

return

self.guessed_letters.append(letter)

if letter in self.word:

print("Good guess!\n")

for i, char in enumerate(self.word):

if char == letter:

self.display_word[i] = letter

else:

print("Wrong guess!\n")

self.lives -= 1

✅ Checks if letter was already guessed.


✅ If correct → updates display.
✅ If wrong → reduces lives.
6️⃣ Check win/lose

def is_won(self):

return '_' not in self.display_word

def is_lost(self):

return self.lives <= 0

These methods check:

 If player has guessed the whole word.


 If player has no lives left.

7️⃣ Game loop

def play(self):

print("=== Welcome to Hangman ===\n")

while not self.is_won() and not self.is_lost():

self.show_progress()

guess = input("Guess a letter: ").lower()

if len(guess) != 1 or not guess.isalpha():

print("Please enter a single valid letter.\n")

continue

self.guess_letter(guess)
if self.is_won():

print(f"🎉 Congratulations! You guessed the word:


{self.word}")

else:

print(f"💀 Game over! The word was: {self.word}")

✅ Repeats until win/lose.


✅ Asks player for input (single letter).
✅ Shows result at the end.

8️⃣ Word list + Start game

words = ['python', 'banana', 'hangman', 'programming',


'challenge']

game = HangmanGame(words)

game.play()

We provide a list of words → create a game object → call the .play() method to
start.

COMBINE CODE
import random

class HangmanGame:

def __init__(self, word_list, lives=6):

self.word = random.choice(word_list).lower()

self.lives = lives

self.guessed_letters = []

self.display_word = ['_' for _ in self.word]

def show_progress(self):

print('Word: ' + ' '.join(self.display_word))

print(f"Lives left: {self.lives}")

print(f"Guessed letters: {',


'.join(self.guessed_letters)}\n")

def guess_letter(self, letter):

if letter in self.guessed_letters:

print("You already guessed that letter.\n")

return

self.guessed_letters.append(letter)
if letter in self.word:

print("Good guess!\n")

for i, char in enumerate(self.word):

if char == letter:

self.display_word[i] = letter

else:

print("Wrong guess!\n")

self.lives -= 1

def is_won(self):

return '_' not in self.display_word

def is_lost(self):

return self.lives <= 0

def play(self):

print("=== Welcome to Hangman ===\n")

while not self.is_won() and not self.is_lost():

self.show_progress()

guess = input("Guess a letter: ").lower()

if len(guess) != 1 or not guess.isalpha():

print("Please enter a single valid letter.\n")


continue

self.guess_letter(guess)

if self.is_won():

print(f"🎉 Congratulations! You guessed the word:


{self.word}")

else:

print(f"💀 Game over! The word was: {self.word}")

words = ['python', 'banana', 'hangman', 'programming',


'challenge']

game = HangmanGame(words)

game.play()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy