Page 139 -
P. 139

add it to the stack


          The rest of the program can’t see

           the password variable


           The program crashed because, for some reason, the program
           couldn’t find a variable called password. But that’s a little odd,
           because you define it in the set_password() function:




                                                 This code sets the password.
                   def set_password():
                       password="C8H10N4O2"
                                                                                      This code uses the
                   set_password()     This code calls for the password to be set.     password... but for
                                                                                      some reason, it can’t
                   def send_to_twitter(msg):                                          see it.
                       password_manager = urllib.request.HTTPPasswordMgr()
                       password_manager.add_password("Twitter API",
                                     "http://twitter.com/statuses", "starbuzzceo", password)





           So what happened? Why can’t the send_to_twitter()
           function see the password variable that was created in the
           set_password() function?
           Programming languages record variables using a section of
           memory called the stack. It works like a notepad. For example,
           when the user is asked if she wants to send a price
           immediately, her answer is recorded against the
           price_now variable:























           104    Chapter 3
   134   135   136   137   138   139   140   141   142   143   144