본문 바로가기
프로그래밍 언어/Python

[python] ValueError: invalid literal for int() with base 10

by nahkim 2023. 5. 12.

에러

파이썬의 형변환 에러이다.

 

에러 발생 코드

import sys
input = sys.stdin.readline

answer = list(map(int, input()))

 

에러 내용

ValueError: invalid literal for int() with base 10: '\n'

 

에러 발생 이유

input(sys.stdin.readline)으로 받은 값은 개행 문자를 포함하고 있기 때문에 개행 문자를 int형으로 변환을 할 수 없어서 생긴 오류이다.

 

해결 방법

answer = list(map(int, input()))

sys.stdin.readline을 사용하지 않고 input으로 받는다!

 

'프로그래밍 언어 > Python' 카테고리의 다른 글

[python] 디렉토리 관련 명령어  (0) 2024.05.27
[python] 파이썬 특징  (0) 2023.05.15
[python] 비동기 I/O asyncio  (0) 2023.05.08
[python] map 함수 사용법  (0) 2023.05.07
[python] 문자열 변경  (0) 2023.05.07