**TIL at 220728
SyntaxError
: 문법 에러
문법 에러가 있는 프로그램은 실행되지 않습니다
SyntaxError
라는 키워드와 함께, 에러의 상세 내용을 보여줍니다.파일이름
과 줄번호
, ^
문자를 통해 파이썬이 코드를 읽어 들일 때(parser
) 문제가 발생한 위치를 표현합니다.parser
는 줄에서 에러가 감지된 가장 앞의 위치를 가리키는 캐럿(caret)기호(^
)를 표시합니다.실행 도중 예상하지 못한 상황(exception)을 맞이했을 때 발생하는 에러
ZeroDivisionError
NameError
TypeError
자료형이 올바르지 못한 경우
1 + '1'
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
함수 호출 과정에서 매개변수 개수가 초과해서 들어온 경우
import random
random.sample([1,2,3])
# TypeError: sample() missing 1 required positional argument: 'k'
함수 호출 과정에서 매개변수 개수가 초과해서 들어온 경우
random.choice([1,2,3], 6)
# TypeError: choice() takes 2 positional arguments but 3 were given
ValueError
자료형은 올바르나 값이 적절하지 않은 경우
int('3.5')
# ValueError: invalid literal for int() with base 10: '3.5'
존재하지 않는 값을 찾고자 할 경우
numbers = [1, 2]
numbers.index(3)
# ValueError: 3 is not in list
IndexError
존재하지 않는 index로 조회할 경우
empty_list = []
empty_list[-1]
# IndexError: list index out of range
KeyError
존재하지 않는 Key로 접근한 경우
songs = {'sia': 'candy cane lane'}
songs['queen']
# KeyError: 'queen'
ModuleNotFoundError
존재하지 않는 Module을 import
하는 경우
import reque # 존재하지 않는 모듈이다.
# ModuleNotFoundError: No module named 'reque'
ImportError
Module은 찾았으나 존재하지 않는 클래스/함수를 가져오는 경우
# random이라는 모듈에서 존재하지 않는 함수를 불러온다.
from random import sampl
# ImportError: cannot import name 'sampl' from 'random'
KeyboardInterrupt
ctrl
+c
를 통해 종료하였을 때 발생IndentationError
Indentation(들여 쓰기)이 적절하지 않은 경우
for i in range(3):
print(i)
"""
Input In [41]
print(i)
^
IndentationError: expected an indented block
"""