title: Python 3 UnboundLocalError: local variable referenced before assignment in try statement
body: <p>I'm trying to make a program that handles RSA encryption and decryption for sending messages using the rsa library. Currently I'm implementing it to use only 1 key pair, but it'll be changed later on. I'm having some trouble with the logic to check whether the files that store the keys exist or not. The logic for these functions looks like so:</p>
  
  <pre><code>import rsa
  
  ################################################
  keySize = 256
  ################################################
  
  
  def genKeys():
      pubkey, privkey = rsa.newkeys(keySize)
  
      with open("public.pem", "w") as pub:
          pub.write(str(pubkey))
  
      with open("private.pem", "w") as priv:
          priv.write(str(privkey))
  
      return pubkey, privkey
  
  def readKeys():
  
        with open("private.pem", "r") as priv:
            keydata = priv.read()
            privkey = rsa.PrivateKey.load_pkcs1(keydata)
            print(privkey)
  
        with open("public.pem", "r") as pub:
            keydata = pub.read()
            pubkey = rsa.PublicKey.load_pkcs1(keydata)
            print(pubkey)
        return pubkey, privkey
  
  
  
  
  def send(message, pubkey):
      message = message.encode('utf-8')
      crypto = rsa.encrypt(message, pubkey)
      return crypto
  
  def recv(crypto, privkey):
      message = rsa.decrypt(crypto, privkey)
      message = message.decode('utf-8')
      return message
  
  def main():
      global pubkey, privkey
      try:
          if (open("private.pem", "r") and open("public.pem", "r")) == True:
  
              pubkey, privkey = readKeys()
  
      except:
          (pubkey, privkey) = genKeys()
  
      cryptMessage = send('hello world', pubkey)
      print(cryptMessage)
      print(recv(cryptMessage, privkey))
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>The <code>main()</code> function, specifically the <code>try: except:</code> statement is where my problem resides. The error I'm getting is <code>builtins.NameError: name 'pubkey' is not defined</code>. I've tried declaring the variables as global, which is what other answers suggested, but that didn't work for me, or I'm doing it wrong. </p>
  
  <p>Thanks for the help. Sorry for the noob question. </p>
  
  <p>EDIT:
  So, that solved my first problem. However, now when I use the <code>try/except</code> statement, it tries to call the variables, which have a value of <code>None</code>, even though I'm assigning them a value using the functions. The stack trace is </p>
  
  <pre><code>File "", line 61, in &lt;module&gt;
    main()
  File "", line 56, in main
    cryptMessage = send('hello world', pubkey)
  File "", line 37, in send
    crypto = rsa.encrypt(message, pubkey)
  File "/usr/local/lib/python3.6/dist-packages/rsa/pkcs1.py", line 169, in encrypt
    keylength = common.byte_size(pub_key.n)
  
  builtins.AttributeError: 'NoneType' object has no attribute 'n'
  </code></pre>