0% found this document useful (0 votes)
80 views

Python Project

This Python program implements a hangman game using ASCII art graphics. It selects a random word from a list, displays partial guesses as underscores, tracks correct and incorrect letters, and ends when either all letters are guessed or the hangman image is complete. The player can choose to play again after completing or losing a round.

Uploaded by

goku spade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Python Project

This Python program implements a hangman game using ASCII art graphics. It selects a random word from a list, displays partial guesses as underscores, tracks correct and incorrect letters, and ends when either all letters are guessed or the hangman image is complete. The player can choose to play again after completing or losing a round.

Uploaded by

goku spade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

source code

# Hangman game with ASCII art.

import random

HANGMAN_PICS = ['''
+---+
|
|
|
===''', '''
+---+
O |
|
|
===''', '''
+---+
O |
| |
|
===''', '''
+---+
O |
/| |
|
===''', '''
+---+
O |
/|\ |
|
===''', '''
+---+
O |
/|\ |
/ |
===''', '''
+---+
O |
/|\ |
/ \ |
===''']
words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow
deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole
monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat
raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad
trout turkey turtle weasel whale wolf wombat zebra'.split()

def getRandomWord(wordList):
"""
Returns a random string from the passed list of strings.

This PDF document was edited with Icecream PDF Editor.


Upgrade to PRO to remove watermark.
"""
wordIndex = random.randint(0, len(wordList) - 1)
return wordList[wordIndex]

def displayBoard(missedLetters, correctLetters, secretWord):


print()
print(HANGMAN_PICS[len(missedLetters)])

print()
print('Missed letters: ', end=' ')
for letter in missedLetters:
print(letter, end=' ')

print()
blanks = '_' * len(secretWord)
for i in range(len(secretWord)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
# Display the secret word with spaces between the letters:
for letter in blanks:
print(letter, end =' ')
print()

def getGuess(alreadyGuessed):
"""
Returns the letter the player entered.
Ensures the player enters a single letter and nothing else.
"""
while True:
print('Please guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Only a single letter is allowed.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a letter from the alphabet.')
else:
return guess

def playAgain():
"""
Returns True if the player wants to play again, False otherwise.
"""
print('Would you like to play again? (y)es or (n)o')
return input().lower().startswith('y')

print('|_H_A_N_G_M_A_N_|')
missedLetters = ''

This PDF document was edited with Icecream PDF Editor.


Upgrade to PRO to remove watermark.
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False

# Now for the game itself:


while True:
displayBoard(missedLetters, correctLetters, secretWord)
# Let the player enter a letter:
guess = getGuess(missedLetters + correctLetters)

if guess in secretWord:
correctLetters = correctLetters + guess
# Check to see if the player has won:
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('You guessed it!')
print('The secret word is "' + secretWord + '"! You win!')
gameIsDone = True
else:
missedLetters = missedLetters + guess

# Check if the player has guessed too many times and lost.
if len(missedLetters) == len(HANGMAN_PICS) -1:
displayBoard(missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + str(len(missedLetters))
+ ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word
was "' + secretWord + '"')
gameIsDone = True
# If the game is done, ask the player to try again.
if gameIsDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord = getRandomWord(words)
else:
break

This PDF document was edited with Icecream PDF Editor.


Upgrade to PRO to remove watermark.
output

This PDF document was edited with Icecream PDF Editor.


Upgrade to PRO to remove watermark.

You might also like