Page 137 -
P. 137
password changes
Someone decided to mess with your code
About time I changed the Twitter
password. Hmmm... Interesting code,
but it would be great if it printed a
message every time it sent a tweet. I
think I‛ll just improve it a little...
One of the Starbuzz coders decided that the password
should be set at the start of the program, where it can be
easily amended in the future. This is what she added:
import urllib.request
import time
def set_password():
This is the new password.
password="C8H10N4O2"
The coder wants to set the
set_password() password at the top of the
file where it’s easy to find.
Use the value of
def send_to_twitter(msg):
“password” here.
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses", "starbuzzceo", password)
So, later in the program, the code uses the password
variable. That means that next time the password needs
to be changed, it will be easier to find it in the code
because it is set right near the top of the file.
102 Chapter 3