import random
HANGMANPICS = ['''
+===+
| |
|
|
|
|
========''','''
+===+
| |
0 |
|
|
|
========''', '''
+===+
| |
0 |
| |
|
|
========''', '''
+===+
| |
0 |
/| |
|
|
========''', '''
+===+
| |
0 |
/|\ |
|
|
========''', '''
+===+
| |
0 |
/|\ |
/ |
|
========''', '''
+===+
| |
0 |
/|\ |
/ \ |
|
========''','''
+===+
| |
[0 |
/|\ |
/ \ |
|
========''','''
+===+
| |
[0] |
/|\ |
/ \ |
|
========''']
words = {'countries': ' usa germany france spain portugal brazil canada russia estonia denmark sweden norway switzerland england greenland iceland afghanistan egypt algeria'.split(), 'things': 'chair table wall ceiling railing stair ladder computer screen building money clothes wheel vehicle xylophone outlet Colin paper note'.split(), 'universes':'ours'.split()}
def getRandomWord(wordDict):
wordKey = random.choice(list(wordDict.keys()))
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)):
if secretWord[i] in correctLetters:
blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
for letter in blanks:
print(letter, end=' ')
print()
def getGuess(alreadyGuessed):
while True:
print('Guess a letter.')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print ('Please enter a single letter.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER.')
else:
return guess
def playAgain():
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 the set: ' + secretKey)
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord:
correctLetters = correctLetters + guess
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('Yes! The secret word is "' + secretWord + '"! You have won!')
gameIsDone = True
else:
missedLetters = missedLetters + guess
if len(missedLetters) == len(HANGMANPICS) -1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
gameIsDone = True
print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + 'missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
if gameIsDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord, secretKey = getRandomWord(words)
else:
break
Comments (0)
You don't have permission to comment on this page.