title: python letter guessing game
body: <p>I'm got the bulk of my first real attempt at a python program--a letter guessing game.</p>
  
  <p>I've got the bulk of the work done, but I'm stuck on the last little bit</p>
  
  <p>I want to make it so that the game alternates back and forth between user and AI turns, until the world has been fully revealed. I'm good so far up to here. At this point, I want to make it so the player to guess the most letters correctly wins a point. The computer moderator picks another word and starts again. The first player to five points wins the game.</p>
  
  <p>I have a while loop that alternates between user/AI turns, but I can't get it to break properly once the word has been fully unveiled? After that it should be pretty simple to just compare the number of userCorrectLetters to the number of aiCorrectLetters and use that to determine who wins the point for the round. </p>
  
  <p>Then I assume the entire thing should go inside a while loop that doesn't break until one of the players has reached 5 points.</p>
  
  <p>The other thing I'm having problems with is how to disallow the user from re-guessing a character position that has already been solved.</p>
  
  <pre><code>import random
  
  
  #set initial values
  player1points= 0
  ai= 0
  userCorrectLetters= []
  aiCorrectLetters=[]
  wrongLetters=[]
  wrongPlace= []
  correctLetters = []
  endGame = False
  allLetters = set(list('abcdefghijklmnopqrstuvwxyz'))
  alreadyGuessed = set() 
  userGuessPosition = 0
  availLetters = allLetters.difference(alreadyGuessed)
  
  
  #import wordlist, create mask
  with open('wordlist.txt') as wordList:
      secretWord = random.choice(wordList.readlines()).strip()
  print (secretWord)
  secretWordLength = len(secretWord)
  
  
  
  
  
  
  
  
  def displayGame():
      mask = '_'  * len(secretWord)
      for i in range (len(secretWord)):
          if secretWord[i] in correctLetters:
              mask = mask[:i] + secretWord[i] + mask [i+1:]
      for letter in mask:
          print (letter, end='')
      print (' ')
      print ('letters in word but not in correct location:', wrongPlace)
      print ('letters not in word:', wrongLetters)
  
  
  
      ##asks the user for a guess, assigns input to variable
  
  def getUserGuess(alreadyGuessed):
  
  
      while True:
          print ('enter your letter')
          userGuess = input ()
          userGuess= userGuess.lower()
          if len(userGuess) != 1:
              print ('please enter only one letter')
          elif userGuess in alreadyGuessed:
              print ('that letter has already been guessed. try again')
          elif userGuess not in 'abcdefjhijklmnopqrstuvwxyz':
              print ('only letters are acceptable guesses. try again.')
          else:
              return userGuess
  
  def newGame():
      print ('yay. that was great. do you want to play again? answer yes or no.')
      return input().lower().startswith('y')
  
  def userTurn(wrongLetters, wrongPlace, correctLetters):
      print ('\n')
  
      displayGame ()
      print ('which character place would you like to guess. Enter number?')
      userGuessPosition = input ()
      if userGuessPosition not in ('123456789'):
          print ('please enter a NUMBER')
          userGuessPosition = input()
      slice1 = int(userGuessPosition) - 1  
  
  
      ##player types in letter
      guess = getUserGuess(wrongLetters + correctLetters)
      if guess== (secretWord[slice1:int(userGuessPosition)]):
          print ('you got it right! ')
          correctLetters.append(guess)
          userCorrectLetters.append(guess)
          displayGame()
  
      elif guess in secretWord:
              wrongPlace.append(guess) 
              print ('that letter is in the word, but not in that position')
              displayGame()
  
      else:
              wrongLetters.append(guess)
              print ('nope. that letter is not in the word')
              displayGame()
  
  
  
  
  def aiTurn(wrongLetters,wrongPlace, correctLetters):
      print ('\n')
      print ("it's the computers turn")
  
      aiGuessPosition = random.randint(1, secretWordLength)
  
      aiGuess=random.sample(availLetters, 1)
      print ('the computer has guessed', aiGuess, "in position", + aiGuessPosition)
      slice1 = aiGuessPosition - 1
      if str(aiGuess) == (secretWord[slice1:userGuessPosition]):
              correctLetters.append(aiGuess)
              aiCorrectLetters.append(aiGuess)
              print ('this letter is correct ')
              return 
      elif str(aiGuess) in secretWord:
              wrongPlace.append(aiGuess)
              print ('that letter is in the word, but not in that position')
              return
  
      else:
              wrongLetters.append(aiGuess)
              print ('that letter is not in the word')
              return
  
  
  
  wordSolved = False 
  while wordSolved == False:
  
      userTurn(wrongLetters, wrongPlace, correctLetters)
      aiTurn(wrongLetters, wrongPlace, correctLetters)
      if str(correctLetters) in secretWord:
          break 
  </code></pre>