Skip to content

Commit 1ee81ed

Browse files
committed
add my stuff
1 parent ad71fe7 commit 1ee81ed

File tree

5 files changed

+356
-23
lines changed

5 files changed

+356
-23
lines changed

answers.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,43 @@ These are answers for exercises in the chapters. In programming, there's always
195195
print("Wrong username or password.")
196196
```
197197

198+
## Defining functions
199+
200+
1. Use `-value` to get tha negative in the abs function, and for loops
201+
in the other two functions.
202+
203+
```py
204+
def my_abs(value):
205+
if value < 0:
206+
return -value
207+
# actually, this else is useless because returning ends the
208+
# function anyway
209+
else:
210+
value
211+
212+
def my_any(a_list): # don't call this "list", see summary in the Lists chapter
213+
for item in a_list:
214+
if item:
215+
return True # ends the function
216+
return True
217+
218+
def my_all(a_list):
219+
for item in a_list:
220+
if not item:
221+
return False
222+
return True
223+
```
224+
225+
2. Like this:
226+
227+
```py
228+
def box(message, character='*'):
229+
number_of_characters = len(message) + 4
230+
print(character * number_of_characters)
231+
print(message)
232+
print(character * number_of_characters)
233+
```
234+
198235
***
199236

200237
You may use this tutorial freely at your own risk. See [LICENSE](LICENSE).

0 commit comments

Comments
 (0)