Sie sind auf Seite 1von 6

19.10.

2016

LearnPythontheHardWayReadforFree

Exerc se 21: Funct ons Can Return Someth ng


You have been us ng the = character to name var ables and set them to numbers or
str ngs. We're now go ng to blow your m nd aga n by show ng you how to use = and a
new Python word return to set var ables to be a value from a function. There w ll be
one th ng to pay close attent on to, but f rst type th s n:

https://learnpythonthehardway.org/book/ex21.html

1/6

19.10.2016

LearnPythontheHardWayReadforFree

def add(a, b):

print "ADDING %d + %d" % (a, b)

return a + b

4
5

def subtract(a, b):

print "SUBTRACTING %d - %d" % (a, b)

return a - b

8
9

def multiply(a, b):

10

print "MULTIPLYING %d * %d" % (a, b)

11

return a * b

12
13

def divide(a, b):

14

print "DIVIDING %d / %d" % (a, b)

15

return a / b

16
17
18

print "Let's do some math with just functions!"

19
20

age = add(30, 5)

21

height = subtract(78, 4)

22

weight = multiply(90, 2)

23

iq = divide(100, 2)

24
25

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

26
27
28

# A puzzle for the extra credit, type it in anyway.

29

print "Here is a puzzle."

30
31

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

32
33

print "That becomes: ", what, "Can you do it by hand?"

https://learnpythonthehardway.org/book/ex21.html

2/6

19.10.2016

LearnPythontheHardWayReadforFree

We are now do ng our own math funct ons for add , subtract , multiply , and
divide . The mportant th ng to not ce s the last l ne where we say return a + b ( n
add ). What th s does s the follow ng:
1

Our funct on s called w th two arguments:

We pr nt out what our funct on s do ng, n th s case "ADDING."

Then we tell Python to do someth ng k nd of backward: we return the add t on of


a + b . You m ght say th s as, "I add a and b then return them."

Python adds the two numbers. Then when the funct on ends, any l ne that runs t
w ll be able to ass gn th s a + b result to a var able.

and

As w th many other th ngs n th s book, you should take th s real slow, break t down,
and try to trace what's go ng on. To help there are extra cred t to solve a puzzle and
learn someth ng cool.

What You Should See


$ python ex21.py
Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50
Here is a puzzle.
DIVIDING 50 / 2
MULTIPLYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes:

-4391 Can you do it by hand?

Study Dr lls
https://learnpythonthehardway.org/book/ex21.html

3/6

19.10.2016

LearnPythontheHardWayReadforFree

If you aren't really sure what return does, try wr t ng a few of your own
funct ons and have them return some values. You can return anyth ng that you
can put to the r ght of an = .

At the end of the scr pt s a puzzle. I'm tak ng the return value of one funct on and
using t as the argument of another funct on. I'm do ng th s n a cha n so that I'm
k nd of creat ng a formula us ng the funct ons. It looks really we rd, but f you run
the scr pt you can see the results. What you should do s try to f gure out the
normal formula that would recreate th s same set of operat ons.

Once you have the formula worked out for the puzzle, get n there and see what
happens when you mod fy the parts of the funct ons. Try to change t on purpose
to make another value.

Do the nverse. Wr te a s mple formula and use the funct ons n the same way to
calculate t.

Th s exerc se m ght really whack your bra n out, but take t slow and easy and treat t
l ke a l ttle game. F gur ng out puzzles l ke th s s what makes programm ng fun, so I'll
be g v ng you more l ttle problems l ke th s as we go.

Common Student Quest ons


Why does Python pr nt the formula or the funct ons "backward"?
It's not really backward, t's " ns de out." When you start break ng down the
funct on nto separate formulas and funct on calls you'll see how t works. Try
to understand what I mean by " ns de out" rather than "backward."

How can I use

raw_input()

to enter my own values?

Remember int(raw_input()) ? The problem w th that s then you can't enter


float ng po nt, so also try us ng float(raw_input()) nstead.

What do you mean by "wr te out a formula"?


Try 24 + 34 / 100 - 1023 as a start. Convert that to use the funct ons. Now
come up w th your own s m lar math equat on and use var ables so t's more
https://learnpythonthehardway.org/book/ex21.html

4/6

19.10.2016

LearnPythontheHardWayReadforFree

l ke a formula.

Buy DRM-Free
When you buy d rectly from the author, Zed A. Shaw, you'll get a profess onal
qual ty PDF and hours of HD V deo, all DRM-free and yours to download and
use as you see f t.
$

29. 99

BUY DIRECTLY FROM THE AUTHOR (HTTPS://PAYDIV.IO/ACCESS/BUY/2/)

OR, YOU CAN READ LEARN PYTHON THE HARD WAY FOR FREE
(HTTPS://LEARNPYTHONTHEHARDWAY.ORG/BOOK/) RIGHT HERE, VIDEO LECTURES NOT INCLUDED.

Other Buy ng Opt ons

BUY ON AMAZON (HTTP://BIT.LY/AMZNLPTHW)

BUY A HARD COPY FROM THE PUBLISHER (HTTP://BIT.LY/INFORMITLPTHW)

https://learnpythonthehardway.org/book/ex21.html

5/6

19.10.2016

LearnPythontheHardWayReadforFree

BUY A HARD COPY FROM BARNES & NOBLE (HTTP://BIT.LY/BNLPTHW)

q Prev ous (ex20.html)

HOME
(/)

Next

ABOUT
(HTTPS://LEARNCODETHEHARDWAY.ORG/ABOUT/)

r (ex22.html)

CONTACT
(HTTPS://LEARNCODETHEHARDWAY.ORG/CONTACT/)

2016 ZED A. SHAW

(https://tw tter.com/lzsthw)

https://learnpythonthehardway.org/book/ex21.html

6/6

Das könnte Ihnen auch gefallen