오류 발생시키기
이번에는 오류 처리의 또 다른 방법을 실습해 보겠습니다.
잘못된 데이터 유형이 사용되었을 때 의도적으로 오류를 발생시키도록 clean_text() 함수를 수정해 보세요.
이 연습은 강의의 일부입니다
개발자를 위한 Python 중급
연습 안내
text인수의 데이터 유형이 문자열str인지 확인하세요.else블록 안에서TypeError()를 발생시켜 스크립트 실행을 중단하고, 상황을 설명하는 메시지를 반환하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
def clean_text(text):
# Check the data type
if ____(text) == ____:
return text.replace(" ", "_").lower()
else:
# Return a TypeError error if the wrong data type was used
____ ____("The clean_text() function expects a string as an argument, please check the data type provided!")
clean_text("User Name 187")