TIL
[TIL] 2024년 11월 14일
dev스카이
2024. 11. 15. 03:31
파이썬
TypeError: slice indices must be integers or None or have an __index__ method
타입이 맞지 않아서 발생한 에러이다. 다음은 위 에러가 난 코드이다.
N = info[0]
for i in info[1:N:2]:
...
- 현재 N은 str 형이다. 그래서 slice 를 사용하려고 할 때 에러가 난 것이다.
- 슬라이싱 할 때는 int 타입만 넣을 수 있다.
해결 방법
N = int(info[0])
- int 형으로 변환하면 에러가 나지 않는다.
📜 작성한 게시글
[SWEA 코딩테스트 3260. 두 수의 덧셈] 👉 https://dev-cloud.tistory.com/409
[SWEA 코딩테스트 4047. 영준이의 카드 카운팅] 👉 https://dev-cloud.tistory.com/410
[SWEA 코딩테스트 16800. 구구단 걷기] 👉 https://dev-cloud.tistory.com/411
[SWEA 코딩테스트 9280. 진용이네 주차타워] 👉 https://dev-cloud.tistory.com/412
[SWEA 코딩테스트 3975. 승률 비교하기] 👉 https://dev-cloud.tistory.com/413