Skip to content

Commit d97dff1

Browse files
committed
way better exercises
1 parent ff8ab6f commit d97dff1

File tree

2 files changed

+82
-91
lines changed

2 files changed

+82
-91
lines changed

answers.md

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -251,39 +251,58 @@ These are answers for exercises in the chapters. In programming, there's always
251251

252252
## Defining functions
253253

254-
1. Use `-value` (it works just like `-1`) to get the negative in
255-
the abs function, and for loops in the other two functions.
254+
1. The problem with the first example is that name is a local variable.
255+
I explained how to fix this in [the output section](defining-functions.md#output):
256256

257257
```py
258-
def my_abs(value):
259-
if value < 0:
260-
return -value
261-
# actually, this else is useless because returning ends the
262-
# function anyway
263-
else:
264-
return value
265-
266-
def my_any(a_list): # don't call this "list", see summary in the Lists chapter
267-
for item in a_list:
268-
if item:
269-
return True # ends the function
270-
return False
271-
272-
def my_all(a_list):
273-
for item in a_list:
274-
if not item:
275-
return False
276-
return True
258+
def ask_name():
259+
name = input("Enter your name: ")
260+
return name
261+
262+
print("Your name is", ask_name())
263+
264+
2. If you run the next example, you get something like this:
265+
266+
<function get_greeting at 0xb73a0a04>
267+
268+
The problem is that we print the actual `get_greeting` function,
269+
but we need to **call** it like `get_greeting()`:
270+
271+
```py
272+
def get_greeting():
273+
return "Hello World!"
274+
275+
print(get_greeting())
276+
```
277+
278+
3. See [the return or print section](defining-functions.html#return-or-print).
279+
280+
The greet function prints a greeting.
281+
282+
```py
283+
>>> greet("World")
284+
Hello World
285+
>>>
277286
```
278287

279-
2. Like this:
288+
But it also returns None because we don't tell it to return anything else.
280289

281290
```py
282-
def print_box(message, character='*'):
283-
number_of_characters = len(message) + 4
284-
print(character * number_of_characters)
285-
print(character, message, character)
286-
print(character * number_of_characters)
291+
>>> return_value = greet("World")
292+
Hello World
293+
>>> print(return_value)
294+
None
295+
>>>
296+
```
297+
298+
This code from the exercise does the same thing as the code above
299+
does, but without a temporary `return_value` variable:
300+
301+
```py
302+
>>> print(greet("World"))
303+
Hello World
304+
None
305+
>>>
287306
```
288307

289308
***

defining-functions.md

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -489,70 +489,42 @@ print("Your favorite color is %s!" % choice)
489489

490490
## Exercises
491491

492-
**TODO:** more exercises.
493-
494-
**There is a lot to learn about functions, and I don't expect you to
495-
learn everything at once.** However, there's also lots of free Python
496-
exercises about defining functions you can do. Do many of them and
497-
spend time with them until you're familiar with defining functions.
498-
499-
1. The box printing function doesn't really print a box, it prints a
500-
message between two lines.
501-
502-
************
503-
Hello World!
504-
************
505-
506-
Modify it to print an actual box:
507-
508-
****************
509-
* Hello World! *
510-
****************
511-
512-
2. Python comes with many built-in functions. Some of the simplest ones
513-
are abs, all and any. They can be used like this:
514-
515-
- abs returns the absolute value of its only argument.
516-
517-
```py
518-
>>> abs(1)
519-
1
520-
>>> abs(-1)
521-
1
522-
>>>
523-
```
524-
525-
- any returns True if one or more of the elements of a list is true,
526-
and False otherwise.
527-
528-
```py
529-
>>> any([True, False, True])
530-
True
531-
>>> any([False, False, False])
532-
False
533-
>>>
534-
```
535-
536-
- all returns True if all elements of a list are true, and False
537-
otherwise.
538-
539-
```py
540-
>>> all([True, True, True])
541-
True
542-
>>> all([True, False, True])
543-
False
544-
>>>
545-
```
546-
547-
Define functions `my_abs`, `my_all` and `my_any` that work the same
548-
way without using the built-in functions. Then run the program with
549-
IDLE, or with `py -i file.py` on Windows or `python3 -i file.py` on
550-
other operating systems. Try the above examples with your
551-
functions.
552-
553-
2. Find more exercises about defining functions online.
554-
555-
Answers for the first and second exercise are [here](answers.md).
492+
**There are many things to learn about functions, and I don't expect
493+
you to learn everything at once.** However, there are also many free
494+
exercises about defining functions you can do.
495+
496+
1. What's wrong with this code?
497+
498+
```py
499+
def ask_name():
500+
name = input("Enter your name: ")
501+
502+
ask_name()
503+
print("Your name is", name)
504+
```
505+
506+
2. How about this code?
507+
508+
```py
509+
def get_greeting():
510+
return "Hello World!"
511+
512+
print(get_greeting)
513+
```
514+
515+
3. Why does this print None after greeting the world?
516+
517+
```py
518+
def greet(target):
519+
print("Hello", target)
520+
521+
print(greet("World"))
522+
```
523+
524+
4. Find more exercises about defining functions online.
525+
526+
Answers for the first, second and third exercise are
527+
[here](answers.md#defining-functions).
556528

557529
***
558530

0 commit comments

Comments
 (0)