Created
September 18, 2016 15:46
-
-
Save pmbaumgartner/7a5374e9006924f917256d357ffe21c1 to your computer and use it in GitHub Desktop.
Hangman (based of TIY assignment)
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
import random | |
from string import ascii_uppercase | |
def get_difficulty(): | |
difficulty_length = {'easy': (4, 6), | |
'medium': (6, 8), | |
'hard': (8, 20) | |
} | |
difficulty_request = input("What's your selected difficulty? EASY, MEDIUM, HARD ? \n> ").lower() | |
while difficulty_request not in ['easy', 'medium', 'hard']: | |
difficulty_request = input( | |
"Please input EASY, MEDIUM, or HARD.\n> ").lower() | |
return difficulty_length[difficulty_request] | |
def load_word(min_length, max_length): | |
capitals = list(ascii_uppercase) | |
with open('/usr/share/dict/words', 'r') as f: | |
words = [i.strip() for i in f.readlines() if len(i.strip()) < max_length and len(i.strip()) > min_length and i[0] not in capitals] | |
selection = random.choice(words) | |
return selection | |
def display_word(word, filled_characters=[]): | |
blank = list("_" * len(word)) | |
if filled_characters is None: | |
display = ' '.join(blank) | |
return display | |
word_characters = list() | |
for filled_character in filled_characters: | |
for i, character in enumerate(word): | |
if character == filled_character: | |
blank[i] = filled_character.upper() | |
display = ' '.join(blank) | |
return display | |
def check_guess(word): | |
character_guess = input("Please guess a character: \n> ") | |
if character_guess in word: | |
return character_guess | |
else: | |
return None | |
def main(): | |
guess_history = [] | |
attempts = 8 | |
difficulty = get_difficulty() | |
selected_word = load_word(difficulty[0], difficulty[1]) | |
win_condition = False | |
print("HINT:", selected_word) | |
print("Your current status:") | |
print(display_word(selected_word)) | |
while attempts != 0 and win_condition == False: | |
guess = check_guess(selected_word) | |
if guess: | |
if guess in guess_history: | |
print("You've already guessed that. Try again") | |
guess = check_guess(selected_word) | |
else: | |
print("You've guessed correct!") | |
guess_history.append(guess) | |
if display_word(selected_word, guess_history).find("_") == -1: | |
print("YOU'VE WON!") | |
print(display_word(selected_word, guess_history)) | |
win_condition = True | |
exit() | |
else: | |
print(display_word(selected_word, guess_history)) | |
attempts -= 1 | |
else: | |
print("Sorry, your character isn't in the word.") | |
print("Current Status:") | |
print(display_word(selected_word, guess_history)) | |
attempts -= 1 | |
print("Out of Attempts, Try again!") | |
exit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment