Use parameters to avoid duplicating functions

Just like it's a bad idea to use copy'n'paste for repeated usages of code, it's also a bad idea to create multiple copies of a function with only minor differences between them. Look again at the proposed send to twitter price low() and send to twitter emergency() functions on the previous page; the only difference between them is the message they send.

A parameter is a value that you send into your function. Think of it as the opposite of what you get when you return a value from a function:

Conned; "to Twi-fc-fcev and send -the message with the pride {o ihe CBO.

Return dontvol to the ¿ailing ¿ode-

Conned; "to Twi-fc-fcev and send -the message with the pride {o ihe CBO.

Return dontvol to the ¿ailing ¿ode-

The parameter's value works just like a variable within the function, except for the fact that its initial value is set outside the function code:

¿this version oi the Wtion, the msg variable is set to the value passed in by the tall to send_to_twitteri'5.5/')".

def send_to_twitter(msg):

password_manager = urllib.request.HTTPPasswordMgr() password_manager.add_password("Twitter API",

"http://twitter.com/s http_handler = urllib.request.HTTPBasicAuthHandler(pas page_opener = urllib.request.build_opener(http_handle urllib.request.install_opener(page_opener) params = urllib.parse.urlencode( {'status': msg} ) resp = urllib.request.urlopen("http://twitter.com/

Parameters Up Close

To use a parameter in Python, simply put a variable name between the parentheses that come after the definition of the function name and before the colon. Then within the function itself, simply use the variable like you would any other:

The parameter «a«>e y>es hev-e. Use the parameter';

/-------value in youv def shout out(the name): 1/ f '

- - +UhdtiOh s ¿ode return("Congratulations " + the_name + "!") just like a»y othev vaviable.

Later, invoke the function from your code with a different parameter value each time you use the function:

print(shout_out('Wanda'))

msg = shout_out('Graham, John, Michael, Eric, and Terry by 2') print(shout_out('Monty'))

Grab your pencil and update your program. Do the following:

1. Modify the send_to_twitter () function so that the message text is passed into the function as a parameter.

2. Update your code to make the approriate parameterized calls to sent to twitter().

ready to tweet

You were to grab your pencil and update your code to incorporate a version of send_to_twitter () that supports parameters:

import urllib.request import time

•«9 variable in the def send_to_twitter(msg): P^eter J

password_manager = urllib.request.HTTPPasswordMgr() password_manager.add_password("Twitter API",

"http://twitter.com/statuses", http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)

page_opener = urllib.request.build_opener(http_handler)

urllib.request.install_opener(page_opener)

params = urllib.parse.urlencode( {'status': msg} )

resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params) resp.read()

page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")

return float(text[start_of_price:end_of_price])

price_now = input("Do you want to see the price now (Y/N)? ")

else price = 99.99 while price > 4.74: time.sleep(900)

price = 99.99 while price > 4.74: time.sleep(900)

Was this article helpful?

0 0

Post a comment