Assignment-2
Name:Subiksha A R A
Sec:11
Roll no:60
Department:ECE
Objective and modules:
Develop a hangman game in Python that randomly generates a word and prompts
the user to guess one letter at a time, as shown in the sample run. Each letter in
the word is displayed as an asterisk. When the user makes a correct guess, the
actual letter is then displayed. When the user finishes a word, display the number
of misses and ask the user whether to continue playing. Create a list to store the
words, as follows:
# Use any words you wish
words = ["write", "that", "program", "rainbow", "computer", "science",
"programming",...]
The game should have the following functions:
a) Getting a random word from string of words # Returns a word
randomly
def pick_a_word():
b) Hiding the characters of chosen random word with * using
suitable iterative structure
c) Making a play function
# Returns win or lose of the game
def play():
d) Constraint: Fix the number of “turns” to be 15. If the guessing character
does not match the word then reduce the “turns” by -1. When turns reach 0, end
the game by displaying “You Loose”.
e) Also, use suitable flag variable to monitor the number of times a user fails
by guessing wrong character
Sample Run:
(Guess) Enter a letter in word *** > p
(Guess) Enter a letter in word p*** > r (Guess) Enter a letter in word prr* > p p is
already in the word
(Guess) Enter a letter in word pr*r* > o
(Guess) Enter a letter in word pro*r** > g (Guess) Enter a letter in word progr** >
n n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr*m > a
The word is "program".You missed 1 time
Do you want to guess another word? Enter y or n>
(i) Develop the user-defined functions for the modules designed in
(ii) Develop the application as a working model by invoking the functions.
(iii) Make use of conditional statements and looping constructs wherever
possible.
(iv) Make use of strings, lists or tuples to play the game.
Python Code:
import random
words = ["write", "that", "program", "rainbow", "computer", "science",
"programming"]
def pick_a_word():
return random.choice(words)
def hide_word(word):
return '*' * len(word)
def play():
word = pick_a_word()
hidden_word = hide_word(word)
turns = 15
missed = 0
guessed_letters = []
while turns > 0 and hidden_word != word:
print("(Guess) Enter a letter in word", hidden_word, ">")
guess = input().lower()
if len(guess) != 1 or not guess.isalpha():
print("Invalid input! Please enter a single letter.")
continue
if guess in guessed_letters:
print("You already guessed that letter! Try a different letter.")
continue
guessed_letters.append(guess)
if guess in word:
for i in range(len(word)):
if word[i] == guess:
hidden_word = hidden_word[:i] + guess + hidden_word[i+1:]
else:
missed += 1
turns -= 1
print(guess, "is not in the word.")
if turns == 0:
print("You lose! The word was", word)
else:
print("You win! The word was", word)
print("You missed", missed, "time(s).")
response = input("Do you want to guess another word? Enter y or n> ")
if response.lower() == 'y':
play()
play()
Output:
(Guess) Enter a letter in word ***** >
p is not in the word.
(Guess) Enter a letter in word ***** >
(Guess) Enter a letter in word *r*** >
You already guessed that letter! Try a different letter.
(Guess) Enter a letter in word *r*** >
m is not in the word.
(Guess) Enter a letter in word *r*** >
o is not in the word.
(Guess) Enter a letter in word *r*** >
u is not in the word.
(Guess) Enter a letter in word *r*** >
(Guess) Enter a letter in word *ri** >
(Guess) Enter a letter in word *ri*e >
n is not in the word.
(Guess) Enter a letter in word *ri*e >
(Guess) Enter a letter in word *rite >
You win! The word was write
You missed 5 time(s).
Do you want to guess another word? Enter y or n> n>
>