Computer Project - Hangman Game
Computer Project - Hangman Game
PROJECT (2020-21)
HANGMAN GAME
ISHAAQ SHAIKH
XI-A
Certificate
________________ ________________
(Internal Examiner) (External Examiner)
ACKNOWLEDGEMENT
SOURCE CODE :
import random, time
fruits = ['pear', 'mango', 'apple', 'banana', 'apricot', 'pineapple','cantaloupe',
'grapefruit','jackfruit','papaya']
superHeroes = ['hawkeye', 'robin', 'Galactus', 'thor', 'mystique', 'superman',
'deadpool', 'vision', 'sandman', 'aquaman']
userGuesslist = []
userGuesses = []
playGame = True
category = ""
continueGame = "Y"
while True:
#Choosing the Secret word
while True:
if category.upper() == 'S':
secretWord = random.choice(superHeroes)
break
elif category.upper() == 'F':
secretWord = random.choice(fruits)
break
else:
category = input("Please select a valid categary: F for Fruits / S for Super-
Heroes; X to exit")
if category.upper() == 'X':
print("Bye. See you next time!")
playGame = False
break
if playGame:
secretWordList = list(secretWord)
attempts = (len(secretWord) + 2)
print("Guess a letter:")
letter = input()
if letter in userGuesses:
print("You already guessed this letter, try something else.")
else:
attempts -= 1
userGuesses.append(letter)
if letter in secretWordList:
print("Nice guess!")
if attempts > 0:
print("You have ", attempts, 'guess left!')
for i in range(len(secretWordList)):
if letter == secretWordList[i]:
letterIndex = i
userGuesslist[letterIndex] = letter.upper()
printGuessedLetter()
else:
print("Oops! Try again.")
if attempts > 0:
print("You have ", attempts, 'guess left!')
printGuessedLetter()
OUTPUT :
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
= RESTART: C:\Users\acer\AppData\Local\Programs\Python\Python39\hangman
game.py
Enter your name ishaaq
Hello ishaaq let's start playing Hangman!
The objective of the game is to guess the secret word chosen by the computer.
You can guess only one letter at a time. Don't forget to press 'enter key' after each
guess.
Let the fun begin!
Please select a valid categary: F for Fruits / S for Super-Heroes; X to exitS
Your Secret word is: ______
The number of allowed guesses for this word is: 8
Guess a letter:
r
Oops! Try again.
You have 7 guess left!
Your Secret word is: ______
Guess a letter:
R
Oops! Try again.
You have 6 guess left!
Your Secret word is: ______
Guess a letter:
h
Oops! Try again.
You have 5 guess left!
Your Secret word is: ______
Guess a letter:
s
Nice guess!
You have 4 guess left!
Your Secret word is: __S___
Guess a letter:
v
Nice guess!
You have 3 guess left!
Your Secret word is: V_S___
Guess a letter:
i
Nice guess!
You have 2 guess left!
Your Secret word is: VISI__
Guess a letter:
o
Nice guess!
You have 1 guess left!
Your Secret word is: VISIO_
Guess a letter:
n
Nice guess!
Your Secret word is: VISION
Yay! you won.
Do you want to play again? Y to continue, any other key to quit