TQST Drivq

Type the code on the opposite page into IDLE, and run it to see what happens:

We» this tode vuks, the pvide is displayed Tight away. (Hoi* you may see a di#eve»-fc pvide when you vu* yOUv tode)

The price appears immediately. You now have a function that reads the contents of the page from the supplier's website and prints out the price information.

You can reuse the function in lots of places in your program simply by calling the get price() function. Now all you have to do is modify your existing program to use the new function.

Look back at the original program at the start of the chapter. You know that you can use this function to produce emergency reports. But it will also need to replace the existing price-watch code. Is there a problem? Why? Why not?

limits of print

Functions are great for reusing code, but they really come into their own when they perform an action for you, then give you back some data to use in whichever way you want.

The current version of the get price () function prints out the price of coffee beans every time it is used, or called. This is OK if that's what you really want it to do. The trouble is, you need the function to give you the price so that you can then decide what you want to do with it.

Was this article helpful?

0 0

Post a comment