Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Hangman
- import random
- import time
- HANGMANPICS = ['''
- +---+
- | |
- |
- |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- | |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- |\\ |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\\ |
- |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\\ |
- / |
- |
- =========''', '''
- +---+
- | |
- O |
- /|\\ |
- / \\ |
- |
- =========''', '''
- +---+
- | |
- [O |
- /|\\ |
- / \\ |
- |
- =========''', '''
- +---+
- | |
- [O] |
- /|\\ |
- / \\ |
- |
- =========''']
- words = {'Colors':'red orange yellow blue green indigo violet white black brown' .split(),
- 'Shapes':'square triangle rectangle circle ellipse rhombus trapezoid chevron pentagon hexagon septagon octagon'.split(),
- 'Fruits':'apple orange lemon lime pear watermelon grape grapefruit cherry banana cantaloupe mango strawberry tomato'.split(),
- 'Animals':'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}
- def getRandomWord(wordDict):
- #This function returns a random string from the dictionary
- #First, selct the key
- wordKey = random.choice(list(wordDict.keys()))
- #Second it selects a word from the key's list
- wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
- return [wordDict[wordKey][wordIndex], wordKey]
- def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
- print(HANGMANPICS[len(missedLetters)])
- print()
- print('Missed letters:', end= ' ')
- for letter in missedLetters:
- print(letter, end= ' ')
- print()
- blanks = '_' * len(secretWord)
- for i in range(len(secretWord)): #replace blanks with correct letters
- if secretWord[i] in correctLetters:
- blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
- for letter in blanks: #show secret word with blanks in between correct letters
- print(letter, end= ' ')
- print()
- def getGuess(alreadyGuessed):
- #returns the letter the player guessed, and makes sure they guessed a letter
- while True:
- print('Take a.')
- guess = input()
- guess = guess.lower()
- if len(guess) != 1:
- print('Guess a SINGLE letter.')
- time.sleep(1)
- print()
- elif guess in alreadyGuessed:
- print('You already tried that one, try something different.')
- time.sleep(1)
- print()
- elif guess not in 'abcdefghijklmnopqrstuvwxyz':
- print('Guess a LETTER.')
- time.sleep(1)
- print()
- else:
- return guess
- def playAgain():
- #This function returns True if the player wants to play again. Otherwise it returns false
- print('Do you want to play again? (Yes or No)')
- return input().lower().startswith('y')
- print('H A N G M A N')
- missedLetters = ''
- correctLetters = ''
- secretWord, secretKey = getRandomWord(words)
- gameIsDone = False
- while True:
- print('The secret word is in category: ' + secretKey)
- displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
- #Let the player guess a letter.
- guess = getGuess(missedLetters + correctLetters)
- if guess in secretWord:
- correctLetters = correctLetters + guess
- #Check if player guessed all the letters
- foundAllLetters = True
- for i in range(len(secretWord)):
- if secretWord[i] not in correctLetters:
- foundAllLetters = False
- break
- if foundAllLetters:
- print('Yes! The secret word was ' + secretWord)
- gameIsDone = True
- else:
- missedLetters = missedLetters + guess
- #check if player has taken too many guessed and lost
- if len(missedLetters) == len(HANGMANPICS) - 1:
- displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
- print('You have run out of guesses! The secret word was ' + secretWord)
- gameIsDone = True
- if gameIsDone:
- if playAgain():
- missedLetters = ''
- correctLetters = ''
- gameIsDone = False
- secretWord, secretKey = getRandomWord(words)
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement