-
-
Save 1328/49bf1bebc150bc2cd569 to your computer and use it in GitHub Desktop.
hang
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
| from random import choice | |
| # reorganize into functions, main | |
| def load_categoties() | |
| with open('words/animals') as a: #Create the pool of words to be randomly chosen from, and split it up into categories. | |
| animal_list = a.read().splitlines() | |
| with open ('words/jobs') as b: | |
| jobs_list = b.read().splitlines() | |
| with open('words/clothes') as c: | |
| clothes_list = c.read().splitlines() | |
| with open('words/countries') as d: | |
| countries_list = d.read().splitlines() | |
| with open('words/food_drink') as e: | |
| food_drink_list = e.read().splitlines() | |
| categories = { | |
| 'animals':animal_list, | |
| 'jobs':job_list, | |
| # etc. | |
| } | |
| return categories | |
| def get_difficulty(): | |
| while True: | |
| print("Please select a difficulty:") | |
| print("Easy (1) Medium (2) Hard (3)") | |
| diff = input("") | |
| if diff in ('1','2','3'): | |
| return diff | |
| def select_word(category, diff): #Easy is less than 6 letters; medium is 6 letters to 8 letters; hard is 9 letters or more. | |
| # use a dictionary to avoid the if/elifs, like this: (note not tested) | |
| # note we use str rather than number to save us the trouble of converting | |
| difficulty_level = { | |
| '1':lambda l:len(l) <6, | |
| '2':lambda l: 5<len(l) <9, | |
| '3':lambda l: len(l) >8, | |
| } | |
| words = [w for w in categoy if difficulty_level[diff](word)] | |
| return choice(words) | |
| def mask_word(word, guesses): | |
| '''return a masked word with guesses filled in''' | |
| return ''.join(l if l in guesses else '_' for l in word) | |
| def get_guess(past_guesses): | |
| while True: | |
| guess = input("Guess a letter: ").upper() | |
| if guess in past_guesses: | |
| print('you already tried that') | |
| continue | |
| if len(guess)>1: | |
| print('one letter at a time') | |
| continue | |
| return guess | |
| def play(word): #Should the whole game be one function? | |
| # definitely break into more functions | |
| guesses = [] | |
| chances = 3 | |
| while chances: | |
| print('you have {} guesses left'.format(chances)) | |
| print(mask_word(word, guesses)) | |
| guess = get_guess(guesses) | |
| guesses.append(guess) | |
| if guess not in word: | |
| chances -= 1 | |
| if all(l in guesses for l in word): | |
| print('you win') | |
| return | |
| print('you lose!') | |
| def main(): | |
| print("Welcome to Hangman! Can you guess all of the letters in the word?") | |
| diffculty = get_difficulty() | |
| categories = load_words() | |
| category, words = choice(list(categories.items())) | |
| print("Your category is %s." % category) | |
| print("You are allowed to guess SIX wrong letters.") | |
| word = select_word(words, difficulty) | |
| play(word) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment