title: Python: How to pass key to a dictionary from the variable of a function?
body: <p>Basically I have two dictionaries: one is a <code>Counter()</code> the other is <code>dict()</code></p>
  
  <p>The first one contains all the unique words in a text, as keys and each words' frequency in the text, as the values</p>
  
  <p>The second contains the same unique words as keys, but the values are the definitions which are user-inputted.</p>
  
  <p>The latter is what I'm having trouble implementing. I created a function which takes in a word, checks if that word is in the frequency dictionary, and if it is, allows the user to input a definition of that word (else, it will print an error). The word and its definition are then added to the second dictionary as a key-value pair (using <code>dict.update(word=definition)</code>).</p>
  
  <p>But whenever I run the program I get the error: </p>
  
  <p>Nameerror: name '' is not defined</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import string
  import collections
  import pickle
  
  freq_dict = collections.Counter()
  dfn_dict = dict()
  
  def cleanedup(fh):
      for line in fh:
          word = ''
          for character in line:
              if character in string.ascii_letters:
                  word += character
              else:
                  yield word
                  word = ''
  
  def process_book(textname):
      with open (textname) as doc:
          freq_dict.update(cleanedup(doc))
      global span_freq_dict
      span_freq_dict = pickle.dumps(freq_dict)
  
  
  def show_Nth_word(N):
      global span_freq_dict
      l = pickle.loads(span_freq_dict)
      return l.most_common()[N]
  
  def show_N_freq_words(N):    
      global span_freq_dict
      l = pickle.loads(span_freq_dict)
      return l.most_common(N)
  
  def define_word(word):
      if word in freq_dict:
          definition = eval(input('Please define ' + str(word) + ':'))
          dfn_dict({word: definition})
      else:
          return print('Word not in dictionary!')
  
  
  
  process_book('DQ.txt')
  process_book('HV.txt')
  
  # This was to see if the if/else was working
  define_word('asdfs')
  #This is the actual word I want to add
  define_word('de')
  
  print(dfn_dict.items())
  </code></pre>
  
  <p>I get the feeling that either the error is very small or very big. Any help would be greatly appreciated.</p>
  
  <p>EDIT: So the program now allows me to enter a definition, but returns this error once I do so:</p>
  
  <pre><code>&gt;&gt;&gt; 
  Word not in dictionary!
  Please define esperar:To await
  Traceback (most recent call last):
    File "C:\Users\User 3.1\Desktop\Code Projects\dict.py", line 50, in &lt;module&gt;
      define_word('esperar')
    File "C:\Users\User 3.1\Desktop\Code Projects\dict.py", line 37, in define_word
      definition = eval(input('Please define ' + str(word) + ':'))
    File "&lt;string&gt;", line 1
      To await
             ^
  SyntaxError: unexpected EOF while parsing
  &gt;&gt;&gt; 
  </code></pre>