Exercises
Functions
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Write a function called double that returns twice its argument, then print double(5).
Python
# define double, then print double(5)
return hands the value back; print only shows it.
def double(n):
return n * 2
print(double(5))Exercise 2Passed
This prints but returns nothing, so result is None. Fix it.
Python
def double(n):
print(n * 2)
result = double(5)
print(result + 1)Return the value instead of printing it inside the function.
def double(n):
return n * 2
result = double(5)
print(result + 1)Exercise 3Passed
Pass the shout function into apply_twice so it is applied twice to hey.
Python
def shout(word):
return word.upper() + "!"
def apply_twice(fn, value):
return fn(fn(value))
# call apply_twice with shout and "hey"
Pass the function without brackets; brackets would call it.
def shout(word):
return word.upper() + "!"
def apply_twice(fn, value):
return fn(fn(value))
print(apply_twice(shout, "hey"))