The Hangman game is a classic word-guessing game where players attempt to guess a hidden word by suggesting letters within a certain number of attempts. This Python implementation of Hangman provides a fun way to test your vocabulary and guessing skills. Players are given hints to help them guess the word, and they score points based on their guesses.
Before running the Hangman game, ensure you have the following:
By working through this project, you will learn:
import random
import time
hints = ["Scary, floats", "Sand and water", "Shows happiness", "Makes things red, blue, etc.", "Stays on top of water", "Challenging to put together", "Lets you see outside", "Speak very softly", "Captures pictures", "Colorful arc after rain", "What you learn and know", "Beautiful flying insect", "Very grand or impressive", "Place you go to eat", "Makes lights work", "What you say to someone who wins", "A chance to do something", "Unusual event", "The words you know", "Being accountable for something",
]
words = ["Ghost", "Beach", "Smile", "Color", "Float", "Puzzle", "Window", "Whisper", "Camera", "Rainbow", "Knowledge", "Butterfly", "Magnificent", "Restaurant", "Electricity", "Congratulations", "Opportunity", "Phenomenon", "Vocabulary", "Responsibility"
]
def dash(word):
global ol
for i in range(len(word)):
print(ol[i], end=" ")
time.sleep(0.2)
print()
def game_on():
ask=input("do you want to play again .enter yes or no.").lower()
if ask=="yes":
return 'yes'
elif ask=="no":
return "break"
else:
print("invalid text.")
return "break"
print("----------WELCOME TO THE GAME OF HANGMAN---------- .\n")
name=input("Enter your name.\n")
print(f"Lets begin {name}!!!\n")
idx=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
while True:
a=random.choice(idx)
idx.remove(a)
print(f"HINT: {hints[a]} \n")
word=words[a].upper()
ol=["_" for i in range(len(word))]
dash(word)
attempt=5
guessed=[]
if "_" not in ol: print(f"you won !! you guessed the word correctly. \nyour score is: {attempt*10} \n")
while attempt> 0:
if "_" in ol:
ans=input("Enter an alphabet or word : \n").upper()
if ans in guessed:
print("you have already guessed the alphabet ! \n")
continue
elif ans==word:
print(f"you guessed the word correctly. \nYour Score is : {attempt*10}\n")
break
else:
guessed.append(ans)
if ans not in word:
attempt=attempt-1
print(f"its wrong !. you have {attempt} chances left \n")
if ans in word:
attempt=attempt-1
for i in range(len(word)):
if word[i]==ans:
ol[i]=ans
print(f"it's correct ! you have {attempt} chances left. \n ")
dash(word)
else:
print("you lost")
print(f"the word was: {word} \nYour score is 0. ")
g=game_on()
if g=="break":
break
import random
import time
hints = ["Scary, floats", "Sand and water", "Shows happiness", "Makes things red, blue, etc.", "Stays on top of water", "Challenging to put together", "Lets you see outside", "Speak very softly", "Captures pictures", "Colorful arc after rain", "What you learn and know", "Beautiful flying insect", "Very grand or impressive", "Place you go to eat", "Makes lights work", "What you say to someone who wins", "A chance to do something", "Unusual event", "The words you know", "Being accountable for something",
]
words = ["Ghost", "Beach", "Smile", "Color", "Float", "Puzzle", "Window", "Whisper", "Camera", "Rainbow", "Knowledge", "Butterfly", "Magnificent", "Restaurant", "Electricity", "Congratulations", "Opportunity", "Phenomenon", "Vocabulary", "Responsibility"
]
def dash(word):
global ol
for i in range(len(word)):
print(ol[i], end=" ")
time.sleep(0.2)
print()
def game_on():
ask=input("do you want to play again .enter yes or no.").lower()
if ask=="yes":
return 'yes'
elif ask=="no":
return "break"
else:
print("invalid text.")
return "break"
print("----------WELCOME TO THE GAME OF HANGMAN---------- .\n")
name=input("Enter your name.\n")
print(f"Lets begin {name}!!!\n")
idx=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
while True:
a=random.choice(idx)
idx.remove(a)
print(f"HINT: {hints[a]} \n")
word=words[a].upper()
ol=["_" for i in range(len(word))]
dash(word)
attempt=5
guessed=[]
if "_" not in ol: print(f"you won !! you guessed the word correctly. \nyour score is: {attempt*10} \n")
while attempt> 0:
if "_" in ol:
ans=input("Enter an alphabet or word : \n").upper()
if ans in guessed:
print("you have already guessed the alphabet ! \n")
continue
elif ans==word:
print(f"you guessed the word correctly. \nYour Score is : {attempt*10}\n")
break
else:
guessed.append(ans)
if ans not in word:
attempt=attempt-1
print(f"its wrong !. you have {attempt} chances left \n")
if ans in word:
attempt=attempt-1
for i in range(len(word)):
if word[i]==ans:
ol[i]=ans
print(f"it's correct ! you have {attempt} chances left. \n ")
dash(word)
else:
print("you lost")
print(f"the word was: {word} \nYour score is 0. ")
g=game_on()
if g=="break":
break
INITIALIZATION:
Import random module
Import time module
Define hints list with corresponding hints
Define words list with words to be guessed
DEFINE HELPER FUNCTIONS:
Function dash(word):
For each character in the word:
Print an underscore with a delay
Function game_on():
Prompt player to enter 'yes' or 'no' to play again
If player enters 'yes', return 'yes'
Else, return 'break'
MAIN GAME LOOP:
Print welcome message
Prompt player to enter their name
Initialize list of indices for words
While there are indices left in the list:
Randomly select an index from the list
Remove selected index from the list
Print the hint for the selected word
Convert selected word to uppercase
Initialize list of underscores for each letter in the word
Call dash(word) to display initial state
Initialize number of attempts to 5
Initialize empty list for guessed letters
Check if word is already guessed:
If yes, print winning message and score
While attempts are greater than 0:
If word is not fully guessed:
Prompt player to guess a letter or word
If guess is already made:
Print message and continue
If guess is correct word:
Print winning message and score
Break loop
Else:
Add guess to guessed list
If guess is incorrect:
Decrement attempts
Print message
If guess is correct letter:
Update list of underscores with guessed letter
Print message
Call dash(word) to display current state
If attempts are 0:
Print losing message, correct word, and score 0
Call game_on() to check if player wants to play again
If player does not want to play again, break loop
INITIALIZATION:
Import random module
Import time module
Define hints list with corresponding hints
Define words list with words to be guessed
DEFINE HELPER FUNCTIONS:
Function dash(word):
For each character in the word:
Print an underscore with a delay
Function game_on():
Prompt player to enter 'yes' or 'no' to play again
If player enters 'yes', return 'yes'
Else, return 'break'
MAIN GAME LOOP:
Print welcome message
Prompt player to enter their name
Initialize list of indices for words
While there are indices left in the list:
Randomly select an index from the list
Remove selected index from the list
Print the hint for the selected word
Convert selected word to uppercase
Initialize list of underscores for each letter in the word
Call dash(word) to display initial state
Initialize number of attempts to 5
Initialize empty list for guessed letters
Check if word is already guessed:
If yes, print winning message and score
While attempts are greater than 0:
If word is not fully guessed:
Prompt player to guess a letter or word
If guess is already made:
Print message and continue
If guess is correct word:
Print winning message and score
Break loop
Else:
Add guess to guessed list
If guess is incorrect:
Decrement attempts
Print message
If guess is correct letter:
Update list of underscores with guessed letter
Print message
Call dash(word) to display current state
If attempts are 0:
Print losing message, correct word, and score 0
Call game_on() to check if player wants to play again
If player does not want to play again, break loop
Challenge: Handling repeated guesses.
Challenge: Ensuring valid inputs.
Challenge: Providing real-time feedback.
Q: Can the game handle repeated guesses?
A: Yes, the game maintains a list of guessed letters and words to ensure repeated guesses are not processed again. Players are informed if they attempt to guess a letter or word that has already been guessed.
Q: How are the guesses validated?
A: Each guess is checked to ensure it is either a single letter or the entire word. Invalid inputs are not processed, and the player is prompted to enter a valid guess.
Q: How is the word display updated?
A: The game uses a function to update the display of the word, showing underscores for unguessed letters and revealing correctly guessed letters in their respective positions.
Q: Can the game be played with more than one player?
A: The game is designed for single-player use. It does not support multiple players or turns in its current implementation.
Q: What happens when the player runs out of attempts?
A: When the player runs out of attempts, the game displays a message indicating they have lost, reveals the correct word, and sets the score to zero.
Q: Can the player play multiple games in a session?
A: Yes, after each game, the player is prompted to decide if they want to play again. If they choose to continue, a new word and hint are selected, and the game restarts.
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India