Created
December 23, 2019 11:05
-
-
Save reloadedd/78e40c5f73de89cf46d063b1028f2bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| ''' | |
| Contents of Practice_Python.functions: | |
| from random import choice | |
| # Used for Hangman series | |
| def pick_random_word() -> str: | |
| """Pick a random number from a list of words.""" | |
| with open('sowpods.txt') as file: | |
| wordlist = file.readlines() | |
| return choice(wordlist) | |
| ''' | |
| from Practice_Python.functions import pick_random_word | |
| WORD = pick_random_word().strip() # Remove all whitespace | |
| if __name__ == '__main__': | |
| print(WORD) | |
| guesses = 0 | |
| build_word = list('_' * len(WORD)) | |
| print('>>> Welcome to Hangman! You have 6 guessed. Good luck!') | |
| print('_ ' * len(WORD)) | |
| while guesses < 6: | |
| guess = input('>>> Guess your letter: ').upper() # Just to make sure | |
| while len(guess) != 1: | |
| guess = input('>>> Guess your letter: ').upper() | |
| if guess in WORD: | |
| i = 0 | |
| for letter in WORD: | |
| if guess == letter: | |
| if build_word[i] == letter: | |
| print('Already guessed. Try another letter.') | |
| # guesses -= 1 | |
| break | |
| else: | |
| build_word[i] = letter | |
| i += 1 | |
| print(' '.join(build_word)) | |
| if '_' not in build_word: | |
| print('Hooray! You guessed the word!') | |
| break # End the game | |
| else: | |
| print('Incorrect!') | |
| guesses += 1 | |
| print('Guesses left: ', (6 - guesses)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment