Avoiding errors
In the video, you saw a couple of approaches for error handling that can be applied to custom functions.
In this exercise, you'll test out one of the approaches that avoids raising an error, printing a helpful message if an error occurs, but not terminating the script.
This exercise is part of the course
Intermediate Python for Developers
Exercise instructions
- Use a keyword allowing you to attempt to run code that cleans
text
. - Swap a space for a single underscore in the
text
argument. - Use another keyword that prints a helpful message if an error occurs when calling the
snake_case()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def snake_case(text):
# Attempt to clean the text
____:
# Swap spaces for underscores
clean_text = ____.____("____", "____")
clean_text = clean_text.lower()
# Run this code if an error occurs
____:
print("The snake_case() function expects a string as an argument, please check the data type provided.")
snake_case("User Name 187")