-
input 자료형 검증 ( try - excpt )Python 2023. 3. 16. 10:54
숫자 두개를 입력받아서 + , - , * , / 할 수 있는 코드를 작성했다.
숫자를 2번 input에 입력하고, operation을 입력하면 해당 식의 결과를 출력해 준다.
operation을 입력할 때 잘 못 된 값이 입력되면 Type correct information을 출력하고 while문을 다시 시작하도록
만들었다.
하지만, 숫자를 입력할 때 숫자가 아닌 다른 데이터를 입력하면 오류가 발생하고 프로그램이 멈췄다.
이를 방지하기 위해 try - except 문을 사용해서 input에 잘 못된 자료형이 입력되었을 때 이를 처리할 수 있도록 설정했다.
try 문에서 input에 int가 아닌 다른 자료형이 입력될 경우 발생하는 에러 ( ValueError )를
except 문에서 예외처리하고 해당 while문을 다시 실행해서 사용자가 올바른 값을 입력할 수 있도록 유도한다.
'Python' 카테고리의 다른 글
User-Agent 지정 ( header ) (0) 2023.03.20 데이터 타입(자료형 검사) (0) 2023.03.18 nomard) python기초 ( ERROR 모음 ) (0) 2023.03.10 nomard) python기초 (Flask로 웹페이지 만들기) (0) 2023.03.10 nomad) python 기초 (2) (0) 2023.03.06