
# 6.00 Problem Set 3
#
# Hangman
#


# -----------------------------------
# Helper code
# (you don't need to understand this helper code)
import random
import string

WORDLIST_FILENAME = "words.txt"

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print "Loading word list from file..."
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r', 0)
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = string.split(line)
    print "  ", len(wordlist), "words loaded."
    return wordlist

def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code
# -----------------------------------

def word_display(guessed, guess, lengthword):
    eles = list(enumerate(guessword))

    for i in eles:


    print eles
    return

# actually load the dictionary of words and point to it with
# the wordlist variable so that it can be accessed from anywhere
# in the program
wordlist     = load_words()
guessword    = choose_word(wordlist)
lengthword   = len(guessword)
numguesses   = 10
iscomplete   = False
letters      = []
guessed      = []
current_word = ""

for i in range(97, 123):
    letters += chr(i)

print "-----------------"
print "Welcome to the game, Hangman!"
print "I am thinking of a word that is %d letters long." % lengthword

while numguesses > 0:
    avlets = ""
    for item in letters:
        avlets += item

    print "Number of guesses: " + str(numguesses)
    print "Available letters: " + avlets
    guess = str.lower(raw_input("Guess a letter: "))
    word_display(guessed,guess, guessword)

    try:
        letters.pop(letters.index(guess))
    except ValueError:
        print "Letter already guessed. Guess Again."
        numguesses += 1
    print "-----------------"


    numguesses -= 1 #TODO: change this depending on answer.



if iscomplete == False:
    print "Sorry!"
    print "GAME OVER"
    print "Correct word was: " + guessword

@@ -916,139 +916,8 @@
 --%0A%0A
-def word_display(guessed, guess, lengthword):%0A    eles = list(enumerate(guessword))%0A%0A    for i in eles:%0A%0A%0A    print eles%0A    return
 %0A%0A# 
@@ -1070,16 +1070,17 @@
 ist     
+ 
 = load_w
@@ -1099,16 +1099,17 @@
 word    
+ 
 = choose
@@ -1137,16 +1137,17 @@
 hword   
+ 
 = len(gu
@@ -1168,16 +1168,17 @@
 esses   
+ 
 = 10%0Aisc
@@ -1187,16 +1187,17 @@
 plete   
+ 
 = False%0A
@@ -1205,24 +1205,25 @@
 etters      
+ 
 = %5B%5D%0Aguessed
@@ -1224,24 +1224,25 @@
 uessed      
+ 
 = %5B%5D%0Acurrent
@@ -1247,20 +1247,39 @@
 nt_word 
+ 
 = %22%22
+%0Acorrect_guess = 0
 %0A%0Afor i 
@@ -1318,16 +1318,60 @@
 = chr(i)
+%0Afor i in guessword:%0A    current_word += %22_%22
 %0A%0Aprint 
@@ -1614,16 +1614,21 @@
  guesses
+ left
 : %22 + st
@@ -1682,16 +1682,57 @@
  avlets%0A
+    print %22Word so far: %22 + current_word%0A
     gues
@@ -1784,48 +1784,736 @@
     
-word_display(guessed,guess, guessword)%0A%0A
+if len(guess) %3E 1:%0A        print %22Only type one letter.%22%0A        print %22-----------------%22%0A    else:%0A        eles = list(enumerate(guessword))%0A        guess_type = False%0A        for i in eles:%0A            for j in i:%0A                try:%0A                    i.index(guess)%0A                except ValueError:%0A                    assert True%0A                else:%0A                    current_word = current_word%5B:(eles%5Bint(i%5B0%5D)%5D)%5B0%5D%5D+ guess + current_word%5B(eles%5Bint(i%5B0%5D)%5D)%5B0%5D+1:%5D%0A                    guess_type=True%0A                    correct_guess += 1%0A                    break%0A        if guess_type == True:%0A            print %22Correct Guess!%22%0A        else:%0A            numguesses -= 1%0A            print %22Wrong guess :(%22%0A    
     
@@ -2509,32 +2509,36 @@
 (%22%0A        try:%0A
+    
         letters.
@@ -2563,24 +2563,28 @@
 guess))%0A    
+ 
+   
 except Value
@@ -2582,32 +2582,36 @@
 ept ValueError:%0A
+    
         print %22L
@@ -2659,117 +2659,119 @@
     
-numguesses += 1%0A    print %22-----------------%22%0A%0A%0A    numguesses -= 1 #TODO: change this depending on answer.%0A%0A
+if correct_guess==lengthword:%0A            iscomplete = True%0A            break%0A        print %22-----------------%22
 %0A%0Aif
@@ -2872,12 +2872,105 @@
 + guessword%0A
+else :%0A    print %22Good job!%22%0A    print %22You Win!%22%0A    print %22Correct word was: %22 + guessword%0A

Complete Hangman