Skip to content

Instantly share code, notes, and snippets.

@FrancoMuniz
Created January 18, 2020 05:26
Show Gist options
  • Select an option

  • Save FrancoMuniz/e2423c31f0fb7e8ea9617d490398b587 to your computer and use it in GitHub Desktop.

Select an option

Save FrancoMuniz/e2423c31f0fb7e8ea9617d490398b587 to your computer and use it in GitHub Desktop.
Hangman Game
import random
def main():
wordList = ["field","shocking","ordinary","tiresome","reminiscent","girls","bulb","willing","curved","brave","poor","comfortable","cloistered","quiver","rescue","sheep","obnoxious","hook","early","guttural","comfortable","quack","suspect","shaggy","clumsy","good","development","worry","silver","hulking","meat","third","sun","flippant","pinch","manage","rod","ratty","base","omniscient","picayune","marked","bow","smelly","belong","envious","crush","bikes","attractive","fair","hurt","belligerent","cakes","hop","apparel","wipe","permit","appreciate","dam","walk","mute"]
selectedWord = wordList[random.randrange(len(wordList)-1)]
print('New Game. Start guessing')
print("".join(selectedWord) + "\n")
currentWord = list("_" * len(selectedWord)) # List, not string
guessedLetters = []
userGuess = ""
userLives = 10
while "".join(currentWord) != selectedWord and userLives > 0:
print('Your lives: {0}'.format(userLives) + ' | ' + 'Current word: {0}'.format("".join(currentWord)))
inputLetter = input('Enter a letter: ')
print('\n')
if len(inputLetter) == 1 and inputLetter.isalpha():
if inputLetter not in guessedLetters:
if inputLetter in selectedWord:
letterIndexes = [i for i, val in enumerate(selectedWord) if val==inputLetter]
for pos in letterIndexes:
currentWord[pos] = inputLetter
print('Correct!')
else:
print('Wrong! Letter "{0}" is not on the word'.format(inputLetter))
userLives -= 1
guessedLetters.append(inputLetter)
else:
print('You already entered letter "{0}"'.format(inputLetter))
else:
print('You can only enter one letter')
if userLives > 0:
print('You won!')
else:
print('You lost! The word was {0}'.format(selectedWord))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment