Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- import time
- def hangman_game(username):
- start = timeit.default_timer()
- print("Hello,", username, ", do you want to play a game of Hangman?")
- while True:
- question = input("Do you want to play a game of Hangman\n(y)/(n):")
- if question == "y":
- break
- elif question == 'n':
- quit()
- print('Initializing Game...\nPlease wait...')
- time.sleep(5)
- game_word = input("Enter the game word:")
- if game_word in [int, float]:
- raise ValueError("No integers, float values are allowed in the game word")
- list_gw = list(game_word)
- word_length = len(list_gw)
- print("The word is", word_length, "characters long")
- lives = 7
- guesses = 0
- player_guess = []
- while lives > 0:
- letter = input("Enter your guess(lowercase only):\n")
- if letter in list_gw:
- indices = [i for i in range(len(list_gw)) if list_gw[i] == letter]
- player_guess.insert(list_gw.index(letter), indices)
- print(indices)
- guesses += 1
- elif letter not in list_gw:
- print("Sorry, there's no instance of this letter")
- guesses += 1
- lives -= 1
- elif letter in [int, float]:
- raise ValueError("The letter must not be an integer or float value")
- if len(player_guess) == word_length:
- print('You WON!')
- stop = timeit.default_timer()
- print('This game took only', round(stop - start), "seconds")
- print('You took', guesses, "guesses")
- break
- def main():
- username = input('Enter your username:')
- print(hangman_game(username))
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment