Guest User

Untitled

a guest
Aug 21st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import timeit
  2. import time
  3.  
  4.  
  5. def hangman_game(username):
  6. start = timeit.default_timer()
  7. print("Hello,", username, ", do you want to play a game of Hangman?")
  8.  
  9. while True:
  10. question = input("Do you want to play a game of Hangman\n(y)/(n):")
  11. if question == "y":
  12. break
  13. elif question == 'n':
  14. quit()
  15.  
  16. print('Initializing Game...\nPlease wait...')
  17. time.sleep(5)
  18.  
  19. game_word = input("Enter the game word:")
  20.  
  21. if game_word in [int, float]:
  22. raise ValueError("No integers, float values are allowed in the game word")
  23.  
  24. list_gw = list(game_word)
  25.  
  26. word_length = len(list_gw)
  27. print("The word is", word_length, "characters long")
  28.  
  29. lives = 7
  30. guesses = 0
  31. player_guess = []
  32.  
  33. while lives > 0:
  34. letter = input("Enter your guess(lowercase only):\n")
  35.  
  36. if letter in list_gw:
  37. indices = [i for i in range(len(list_gw)) if list_gw[i] == letter]
  38. player_guess.insert(list_gw.index(letter), indices)
  39. print(indices)
  40. guesses += 1
  41. elif letter not in list_gw:
  42. print("Sorry, there's no instance of this letter")
  43. guesses += 1
  44. lives -= 1
  45. elif letter in [int, float]:
  46. raise ValueError("The letter must not be an integer or float value")
  47.  
  48. if len(player_guess) == word_length:
  49. print('You WON!')
  50. stop = timeit.default_timer()
  51. print('This game took only', round(stop - start), "seconds")
  52. print('You took', guesses, "guesses")
  53. break
  54.  
  55.  
  56. def main():
  57. username = input('Enter your username:')
  58. print(hangman_game(username))
  59.  
  60.  
  61. if __name__ == '__main__':
  62. main()
Add Comment
Please, Sign In to add comment