Error handling with try-except
A good practice in writing your own functions is also anticipating the ways in which other people (or yourself, if you accidentally misuse your own function) might use the function you defined.
As in the previous exercise, you saw that the len()
function is able to handle input arguments such as strings, lists, and tuples, but not int type ones and raises an appropriate error and error message when it encounters invalid input arguments. One way of doing this is through exception handling with the try-except
block.
In this exercise, you will define a function as well as use a try-except
block for handling cases when incorrect input arguments are passed to the function.
Recall the shout_echo()
function you defined in previous exercises; parts of the function definition are provided in the sample code. Your goal is to complete the exception handling code in the function definition and provide an appropriate error message when raising an error.
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Initialize the variables
echo_word
andshout_words
to empty strings. - Add the keywords
try
andexcept
in the appropriate locations for the exception handling block. - Use the
*
operator to concatenateecho
copies ofword1
. Assign the result toecho_word
. - Concatenate the string
'!!!'
toecho_word
. Assign the result toshout_words
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define shout_echo
def shout_echo(word1, echo=1):
"""Concatenate echo copies of word1 and three
exclamation marks at the end of the string."""
# Initialize empty strings: echo_word, shout_words
# Add exception handling with try-except
____:
# Concatenate echo copies of word1 using *: echo_word
echo_word = ____
# Concatenate '!!!' to echo_word: shout_words
shout_words = ____
____:
# Print error message
print("word1 must be a string and echo must be an integer.")
# Return shout_words
return shout_words
# Call shout_echo
shout_echo("particle", echo="accelerator")