#! python3
# hangman.py
import random, logging
'------------------------------------------------------------------------------'
# Sets up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)03d: %(message)s', datefmt='%H:%M:%S')
logging.disable(logging.DEBUG) # uncomment to block debug logging.debug messages
#logging.disable(logging.INFO) # uncomment to block debug logging.info messages
def hangman():
# Sets local variables
answerChoice_list = ['hello', 'world', 'house', 'tall', 'ingenuity', 'apple', 'coffee','apartment']
answerWord_str = answerChoice_list[random.randint(0, len(answerChoice_list)-1)]
answerLen_int = len(answerWord_str)
logging.info('answerWord_str: %s' % answerWord_str)
logging.debug('answerLen_int: %d' % answerLen_int)
match_list = [] # a list that will hold all the matches between user guesses and answer letters
logging.debug('match_list length: %d' % len(match_list))
letterGuess_str = '' # initalizs variable to hold the user guess
winState = False # Holds the overall win state of the game
maxGuess_int = 5 # maxium number of guess allowed
guess_iter = 0 # keeps track of number of guess
#Beginning state of game
print('This is a game of hangman guess all the letters in a hidden word you have %d strikes to guess the answer.' % maxGuess_int )
print('Guess a letter')
# main while loop for game
while guess_iter < maxGuess_int: # continues as long as max guesses not reached
letterGuess_str = input()
letterCount_int = 0
#Error checking section
# Check that letterGuess is one letter long
while len(letterGuess_str) > 1:
print('ERROR: you must input a stingle letter')
letterGuess_str = input()
# Check that correct letterGuess has not already been found
while letterGuess_str in match_list:
print('ERROR: letter already found guess again.')
letterGuess_str = input()
# game logic
if letterGuess_str.lower() in answerWord_str: # if match found
print('MATCH FOUND:letter found in word')
for l in answerWord_str: # Checks number of time letter guessed appears in answer word
if l == letterGuess_str.lower():
letterCount_int += 1
logging.debug('letterCount_int: %d' % letterCount_int)
for i in range(0,letterCount_int):
match_list.append(letterGuess_str.lower())
if letterGuess_str.lower() not in answerWord_str: # if match NOT found
print('NO MATCH: no match found')
guess_iter += 1 # iterates guess counter
if len(match_list) == answerLen_int: # switches winState and breaks while loop if user guess all the letters
winState = True
break
# Outputs feedback for user
print('\tGuesses remaining: %d' % (maxGuess_int-guess_iter)) # tells user number of guess remaining
print('\tLetters Found:{matches}'.format(matches=match_list))
logging.debug(match_list)
# prints next guess prompt
if maxGuess_int-guess_iter != 0:
print('\nGuess another letter')
# End state condition evaluation
if winState == True:
print('\nWIN: answer is "%s"' % answerWord_str )
else:
print('\nLOST: answer is "%s"' % answerWord_str )
print('DONE:Exiting program')
'------------------------------------------------------------------------------'
#Main Call
if __name__ == '__main_':
hangman()