Advertisement
Guest User

Hangman in Python

a guest
Nov 19th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import random
  4.  
  5. words = ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
  6.  
  7. word = list(random.choice(words))
  8. guesses = ['_'] * len(word)
  9.  
  10. lives = 3
  11.  
  12. while lives > 0:
  13.     if guesses == word:
  14.         print("You win! Word was ā€œ%sā€" % ''.join(word))
  15.         break
  16.  
  17.     print(guesses)
  18.     print("Lives: %d" % lives)
  19.     guess = input("Enter a character: ")
  20.  
  21.     if len(guess) != 1:
  22.         print ("I said A (single) character!")
  23.         continue
  24.  
  25.     guess_locations = [i for i, val in enumerate(word) if val == guess]
  26.     if len(guess_locations) > 0:
  27.         for g in guess_locations:
  28.             guesses[g] = guess
  29.     else:
  30.         print('nope!')
  31.         lives -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement