0% found this document useful (0 votes)
82 views4 pages

Hangman Game

This document contains the source code for a Python hangman game. The code includes functions for playing the game like hangman(), which takes a random word as input and handles guessing letters. It also tracks number of incorrect guesses and prints hangman art. Additional functions handle playing again and the main game loop. The game picks a random capital city name for the player to guess letter by letter until they win or reach 5 incorrect guesses.

Uploaded by

abbas abbas
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)
82 views4 pages

Hangman Game

This document contains the source code for a Python hangman game. The code includes functions for playing the game like hangman(), which takes a random word as input and handles guessing letters. It also tracks number of incorrect guesses and prints hangman art. Additional functions handle playing again and the main game loop. The game picks a random capital city name for the player to guess letter by letter until they win or reach 5 incorrect guesses.

Uploaded by

abbas abbas
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/ 4

ICS PROJECT

Submitted To: Sir Khalil - ur – Rehman


By : Muhammad Aun Ali & Muhammad Ali

Python Hangman Game


Source Code:
'''
Hangman Game
-------------------------------------------------------------
'''

import random
import time
import os

def play_again():
question = 'Do You want to play again? y = yes, n = no \n'
play_game = input(question)
while play_game.lower() not in ['y', 'n']:
play_game = input(question)

if play_game.lower() == 'y':
return True
else:
return False

def hangman(word):
display = '_' * len(word)
count = 0
limit = 5
letters = list(word)
guessed = []
while count < limit:
print("Hint : Capitals of Asia")
print("-------------------------------------")
guess = input(f'Hangman Word: {display} Enter your guess: \n').strip()
while len(guess) == 0 or len(guess) > 1:
print('Invalid input. Enter a single letter\n')
guess = input(
f'Hangman Word: {display} Enter your guess: \n').strip()
if guess in guessed:
print("-------------------------------------")
print('Oops! You already tried that guess, try again!\n')
continue

if guess in letters:
letters.remove(guess)
index = word.find(guess)
display = display[:index] + guess + display[index + 1:]

else:

guessed.append(guess)
count += 1
if count == 1:
time.sleep(1)
print("-------------------------------------")
print(' _____ \n'
' | \n'
' | \n'
' | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 2:
time.sleep(1)
print("-------------------------------------")
print(' _____ \n'
' | | \n'
' | | \n'
' | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 3:
time.sleep(1)
print("-------------------------------------")
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 4:
time.sleep(1)
print("-------------------------------------")
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | O \n'
' | \n'
' | \n'
'__|__\n')
print(f'Wrong guess: {limit - count} guesses remaining\n')

elif count == 5:
time.sleep(1)
print("-------------------------------------")
print(' _____ \n'
' | | \n'
' | | \n'
' | | \n'
' | O \n'
' | /|\ \n'
' | / \ \n'
'__|__\n')
print('Wrong guess. You\'ve been hanged!!!')
print("-------------------------------------")
print(f'The word was: {word}')
print("-------------------------------------")

if display == word:
print("--------------------------------------------------\n")
print(f'Congrats! You have guessed the word \'{word}\' correctly!',"\U0001F60A")
break

def play_hangman():
print('Welcome to Hangman')
print("-------------------------------------------- ")
name = input('Enter your name: ')
print("-------------------------------------------- ")
print(f'Hello {name}! Best of Luck!'," \U0001f600")
time.sleep(1)
print("--------------------------------------------")
print('The game is about to start!\nLet\'s play Hangman!')
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
words_to_guess = ['dehli', 'kabul','seoul', 'tehran',
'riyadh','muscat','berlin','rome','paris'
'doha']
play = True
while play:
word = random.choice(words_to_guess)
long = len(word)
print(f"\n Word to Guess has {long} Characters")
hangman(word)
play = play_again()

print('\n Thanks For Playing! We expect you back again!',"\U0001F636")


exit()

if __name__ == '__main__':
play_hangman()

You might also like