Write a simple function
In the last video, Hugo described the basics of how to define a function. You will now write your own function!
Define a function, shout()
, which simply prints out a string with three exclamation marks '!!!'
at the end. The code for the square()
function that we wrote earlier is found below. You can use it as a pattern to define shout()
.
def square():
new_value = 4 ** 2
return new_value
Note that the function body is indented 4 spaces already for you. Function bodies need to be indented by a consistent number of spaces and the choice of 4 is common.
This course touches on a lot of concepts you may have forgotten, so if you ever need a quick refresher, download the Python for Data Science Cheat Sheet and keep it handy!
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Complete the function header by adding the appropriate function name,
shout
. - In the function body, concatenate the string,
'congratulations'
with another string,'!!!'
. Assign the result toshout_word
. - Print the value of
shout_word
. - Call the
shout
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define the function shout
def ____():
"""Print a string with three exclamation marks"""
# Concatenate the strings: shout_word
# Print shout_word
print(____)
# Call shout