Return data with the return command

If you use the return () command within a function, you can send a data value back to the calling code.

"n -the dode "to determine the duv-v-ent pride.

The value assigned b> Vite is S.SI. The assignment happens after -the ¿ode in •the iunttion e*edu-tes.

The value assigned b> Vite is S.SI. The assignment happens after -the ¿ode in •the iunttion e*edu-tes.

"n -the dode "to determine the duv-v-ent pride.

don't repeat yourself

-tJiereiiffe no

Dumb Questions

Q/ The return() command is just like print(), except nothing appears on screen, right?

A" Well... sort of. The print () command is designed to display (or output) a message, typically on screen. The return () command is designed to allow you to arrange for a function you write to provide a value to your program. Recall the use of randint () in Chapter 1: a random number between two values was returned to your code. So, obviously, when providing your code with a random number, the randint () function uses return () and not print (). In fact, if randint () used print () instead of return (), it would be pretty useless as a reusable function.

Q/ So, it's a case of return() letting a function give you something back?

I'm not sure I'm convinced about using functions. Isn't using copy'n'paste quick and easy?

A." No, using copy'n'paste is quick and dirty, with the emphasis on the "dirty." When you need to repeatedly use some code, it's always better to create a function to contain and name that code. You then call (or invoke) the function as needed. If you later decide to change how the repeated code works, it's a no-brainer to change the code in the function once. If, instead, you "quickly" performed copy'n'paste five times, that's five changes you now have to make, and the chance of you missing one change or making a mistake are actually pretty high. So, don't copy'n'paste!

So, using a function lets you share the repeated code in a controlled way?

A." Yes, it does. There's also a guiding principle among prgrammers known as DRY: Don't Repeat Yourself. Using functions lets you keep your code DRY.

What happens if the function omits the return() command? Does each function have to have one?

A." No, the use of return () is not required. In fact, the current version of your get_price () function doesn't use return () at all. But, your function feels like it gives you something because it prints the current price on screen. When the return () command is omitted, a function returns a special no value. In Python, this value is called None.

So, just to be clear, using return() is optional?

Does return() always come at the end of the function?

A." Usually, but this is not a requirement, either. The return () can appear anywhere within a function and, when it is executed, control returns to the calling code from that point in the function. It is perfectly reasonable, for instance, to have multiple uses of return () within a function, perhaps embedded with i f statements which then provide a way to control which return () is invoked when.

Can return() send more than one result back to the caller?

A." Yes, it can. return () can provide a list of results to the calling code. But, let's not get ahead of ourselves, because lists are not covered until the next chapter. And there's a little bit more to learn about using return () first, so let's read on and get back to work.

Using the new get_price () function, write a new version of the price-checking program that does this:

1. Ask the user to indicate if the price is required immediately (Y/N).

2. If the user chooses "Y" for "yes," find the current price and display it on the screen.

3. Otherwise, check the price every 15 minutes until it falls below $4.74, then (and only then), display the price on screen.

buy it now

Using the new get_price () function, you were asked to write a new version of the price-checking program that does this:

1. Ask the user to indicate if the price is required immediately (Y/N).

Your Code may Uk a little arfUtU this, but that s 0K. As long as it does the same thing, you're doing -fine-

2. If the user chooses "Y" for "yes," find the current price and display it on the screen.

3. Otherwise, check the price every 15 minutes until it falls below $4.74, then (and only then), display the price on screen.

import urllibrequest import time def. 3e.t_price();

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

text = page.readO.deCodeatf«")

where = textfindC>f')

return float(textCstart_of_price:end_of_priceJ)

if the pride is required priCe_now = input("Do you want to see the price now (Y/N)? ") immediately.

Wtion gives you-

price = 9999

while price > 474:

1+ the user decides to wait for

time.sleep(900)

using the get_price0 function, , ""

Was this article helpful?

0 0

Post a comment