Get startedGet started for free

Returning errors

Time to try out the other approach for error handling.

Revise the snake_case() function to intentionally produce an error if an incorrect data type is used.

This exercise is part of the course

Intermediate Python for Developers

View Course

Exercise instructions

  • Check whether the data type of the text argument is a string str.
  • Inside the else block, produce a TypeError() to prevent the script running and return a descriptive message.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def snake_case(text):
  # Check the data type
  ____:
    clean_text = text.replace(" ", "_")
    clean_text = clean_text.lower()
  else:
    # Return a TypeError error if the wrong data type was used
    ____("The snake_case() function expects a string as an argument, please check the data type provided.")
    
snake_case("User Name 187")
Edit and Run Code