2025.04.23 - [coding test/백준] - [Python] 2667번 단지번호붙이기
[Python] 2667번 단지번호붙이기
https://www.acmicpc.net/problem/26672667번 문제는 저번에도 풀어봤고 이번에 다시 복습해보았다. 확실히 한 번 풀었어서 그런지 빨리 풀렸다. import sysinput = sys.stdin.readline#bfsdy=[0,1,-1,0]dx=[-1,0,0,1]def bfs(y,x,re
wish404.tistory.com
해당 문제를 풀면서 input()과 sys.stdin.readline()의 차이로 에러가 발생했었다.
에러가 발생했던 코드:
import sys
input = sys.stdin.readline
graph.append(list(map(int, input())))
이렇게 하면 input()은 줄 끝에 \n(엔터 문자)도 포함한 문자열을 받아오게 된다.
ex) 왜냐면 마지막 입력값을 치게 되면서 엔터를 누르게 되므로 입력값이 1234라면 input()값은 '1234'+'\n'가 된다.
input() == '1234\n'
list(map(int, input())) → [1, 2, 3, 4, ???] ❌
→ 여기서 \n도 문자 하나로 포함되니까
int('\n')을 시도하게 되고, ValueError가 발생 ❗
이를 해결하기 위해 하기 위해서는 공백 문자를 제거해주는 strip()를 쓴다.
✅ 해결 방법: .strip() 사용
input() == '1234\n'
input().strip() == '1234'
map(int, input().strip()) → [1, 2, 3, 4] ✅

'coding test > etc' 카테고리의 다른 글
| [Python] heapq 라이브러리 (0) | 2025.05.22 |
|---|---|
| Shallow Copy Issue (0) | 2025.02.24 |