TQST Drivq

Now you've amended the program code, it's time to see if it works. Make sure the amended code is in IDLE, and then press F5 to run your program.

To begin, let's send an emergency message:

asked -for an emerge^y pride here it is.

That worked. But what about the price-watch option...?

message received message received

You decide to wait and (eventually) the tweet with a message to buy • comes thru (when the price is right).

That works as well. You're ready to go live!

Beep, beep- w "that someone's phone?

No matter where the Starbuzz. CBO is, \( he has his dell nearby, he gets the message.

iJierejflre no

Dumb Questions

Q/ Can I still call the Twitter function like this: send_to_ twitter()? Or do I always have to provide a value for the msg parameter?

A" As it's written, the parameter is required by the function. If you leave it out, Python will complain and refuse to run your code further.

Can parameters to functions be optional?

A" Yes. In most programming languages (including Python), you can provide a default value for a parameter, which is then used if the calling code doesn't provide any value. This has the effect of making the parameter optional, in that it either takes its value from the one provided by the caller, or uses the default value if the caller does not provide anything.

Can there be more than one parameter?

A." Yes, you can have as many as you like. Just bear in mind that a function with a gazillion parameters can be hard to understand, let alone use.

Can all the parameters be optional?

A." Yes. As an example, Python's built-in print () function can have up to three optional parameters, in addition to the stuff to print (which is also optional). To learn more, open up a Python Shell prompt and type help (print) at the >>> prompt.

Doesn't all that optional stuff get kinda confusing?

A." Sometimes. As you create and use functions, you'll get a feel for when to make parameters mandatory and when to make them optional. If you look at the description of print () again , you'll see that in most usage scenarios print () takes a single parameter: the thing to display. It is only when extra, less common, functionality is required that the other parameters are needed.

The description of print() mentions "keyword arguments." What are they?

A." The word "argument" is another name for "parameter," and it means the same thing. In Python, an argument can have an optional "keyword" associated with it. This means that the parameter has been given a name that the calling code can use to identify which value in its code is associated with which parameter in the function.

Continuing to use print () as an example, the sep, end, and file parameters (a.k.a. keyword arguments) each have a default value, so they are all optional. However, if you need to use only one of them in the calling code, you need some way to identify which one you are using, and that's where the keyword arguments come in. There are examples of these optional features of print () and other such functions later in the book. Don't sweat the details right now, though.

password changes

Was this article helpful?

0 0

Post a comment