Created
January 26, 2018 09:30
-
-
Save bjmc/284a731c01470a628b2f424bbb41bf22 to your computer and use it in GitHub Desktop.
Revisions
-
bjmc created this gist
Jan 26, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,175 @@ import random # List of words some_words = [ 'aloof', 'radiate', 'apathetic', 'inestimable', 'coil', 'immense', 'reading', 'unequal', 'tangy', 'detail', 'spurious', 'nosy', 'furtive', 'call', 'miscreant', 'obeisant', 'mice', 'succeed', 'disagree', 'snotty', 'circle', 'testy', 'soup', 'valuable', 'half' ] def draw_hangman(tries): # This function draws a hangman that looks like this: # ________ # |/ | # | (_) # | \|/ # | | # | / \ # | # _|___ # Copied from http://ascii.co.uk/art/hangman # First line, part of the scaffold: print " ________ " # Second line, more scaffold with or w/out rope: if tries < 7: print " |/ | " else: print " |/ " # Third line, head if tries < 7: print " | (_)" else: print " | " # Forth line, neck + two arms if tries < 4: print " | \|/" elif tries < 5: print " | \| " elif tries < 6: print " | | " else: print " | " # Fifth line, body if tries < 3: print " | | " else: print " | " # legs if tries < 1: print " | / \ " elif tries < 2: print " | / " else: print " | " # Last bit of scaffold, always shows: print " |" print " _|___" def pick_a_word(possible_words): return random.choice(possible_words).upper() def show_banner(): # Borrowed from http://ascii.co.uk/art/hangman # We're getting a bit fancy here # The three quotes make this a 'multi-line string' # the letter 'r' means it's a 'raw' string print r""" _ | | | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ | '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ | | | | (_| | | | | (_| | | | | | | (_| | | | | |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_| __/ | |___/ """ def check_guess(letter, the_word): # We could also just do: # `return guess in the_word` if letter in the_word: return True else: return False def show_guesses(user_guessed): print "So far, you've tried: {}".format(user_guessed) def ask_user_for_guess(): letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # Ask the user for a guess; make whatever she tells us upper-case. guess = raw_input("Guess a letter: ").upper() # Use a loop here in case our user is stubborn while True: # Some basic error handling if our user gets tricky. if len(guess) > 1 or guess not in letters: guess = raw_input("Enter one letter, please: ").upper() else: # Get out of this error-checking loop break return guess def hide_letters(unhidden_word, guesses_so_far): # This is a list we'll use to build the word with # some letters hidden. hidden_word = [] for letter in unhidden_word: if letter in guesses_so_far: # Show this letter hidden_word.append(letter) else: # Still hidden: hidden_word.append('_') # ' '.join() here takes each element of a list # and puts it together, separated by spaces. return ' '.join(hidden_word).upper() # Set up some stuff before the game begins: tries_remaining = 7 guesses_so_far = [] the_word = pick_a_word(some_words) hidden_word = hide_letters(the_word, guesses_so_far) show_banner() while tries_remaining > 0: draw_hangman(tries_remaining) print "Your word is: {}".format(hidden_word) show_guesses(guesses_so_far) guess = ask_user_for_guess() # Add this guess to the list of previous guesses guesses_so_far.append(guess) hidden_word = hide_letters(the_word, guesses_so_far) # Are there any hidden letters left? Has the user won? if '_' not in hidden_word: print hidden_word print "You win!" # Exit the loop & the whole program, no more guessing exit() correct = check_guess(guess, the_word) if not correct: # Take away one of the user's tries: tries_remaining = tries_remaining - 1 # Here we're outside of the loop naturally, because our user # has used up her tries. print "GAME OVER" draw_hangman(tries_remaining)