title: Python - Tkinter: error saying my index is out of range, when there is
body: <p>So, I am making this hangman game for a school project, and have come across a problem, that I just cannot for the life of me figure out even <em>why</em> it happens.</p>
  
  <pre><code>word_list = ["APPLE", "PEAR", "BANNANA"]
  
  word = word_list [random.randint(0,2)]
  
  hidden_word = ["_ " * len(word)]
  print (word)
  </code></pre>
  
  <p>This bit of code is a list, and then one of the words made into a string variable:</p>
  
  <pre><code>word = word_list [random.randint(0,2)]
  </code></pre>
  
  <p>I then make a new list that is the hidden word, with '_' used to hide, by getting the length:</p>
  
  <pre><code>hidden_word = ["_ " * len(word)]
  </code></pre>
  
  <p>I then print the word (for development)</p>
  
  <pre><code>print (word)
  </code></pre>
  
  <p>On to the problematic code.</p>
  
  <pre><code>def click_1 (key):
      if str(key) in word:
          key_1 = word.index(key)
          print (key_1)
          hidden_word[key_1] = key
          print (hidden_word)
      else:
          print ("Nope")
      return letter
  
  r = c = 0
  for letter in string.ascii_uppercase:
      Button(letter_frame, text=letter, command=functools.partial(click_1, letter)).grid(row=r, column=c, sticky=W)
      c += 1
      if c &gt; 12:
          c = 0
          r += 1
  </code></pre>
  
  <p>This makes butons come up, and when I click on the button with the letter, it checks if it's in the word, and then (at the moment) prints:</p>
  
  <pre><code>BANNANA
  &gt;&gt;&gt; 0
  ['B']
  </code></pre>
  
  <p>if the word is bannana. The problem is when I press the A:</p>
  
  <pre><code>1
  </code></pre>
  
  <p>comes up, and if I press something else, this error appears:</p>
  
  <pre><code>Exception in Tkinter callback
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/run.py", line 121, in main
      seq, request = rpc.request_queue.get(block=True, timeout=0.05)
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/queue.py", line 175, in get
      raise Empty
  queue.Empty
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1475, in __call__
      return self.func(*args)
    File "/Users/alexeacott/Desktop/Hangman.py", line 24, in click_1
      hidden_word[key_1] = key
  IndexError: list assignment index out of range
  </code></pre>
  
  <p>The last line is of most intersest, because apparantly, N is out of range. My question is, why is this happening, and what could I do to fix.</p>
  
  <p>Happy Holidays!</p>