먼저 생각하기 · 기초
다음 모습 예상하기: 한 번 연 문으로 읽고 고쳐 써요
생활 장면은 이래요. 입고표의 첫 글자를 읽은 뒤 바로 그 자리에서 마지막 표시를 고치려 해요. 다음 모습을 먼저 예상하고 짧은 Python 코드, 즉 컴퓨터가 따라 할 글로 확인합니다.
아래 글을 아직 컴퓨터가 따라 하게 하지 말고, 마지막에 보일 모습과 그렇게 생각한 까닭을 먼저 적으세요. from pathlib import Path from tempfile import TemporaryDirectory with TemporaryDirectory() as room: note = Path(room) / "today.txt" note.write_text("ABC", encoding="utf-8") with note.open("r+", encoding="utf-8") as handle: first = handle.read(1) handle.seek(2) handle.write("Z") print(first, note.read_text(encoding="utf-8"))
- 첫 글: from pathlib import Path from tempfile import TemporaryDirectory with TemporaryDirectory() as room: note = Path(room) / "today.txt" note.write_text("ABC", encoding="utf-8") with note.open("r+", encoding="utf-8") as handle: first = handle.read(1) handle.seek(2) handle.write("Z") print(first, note.read_text(encoding="utf-8"))
- 공개 풀이를 보기 전에 예상과 그렇게 생각한 까닭을 먼저 적습니다.
- 실제 이름·연락처·계정 정보 대신 수업용 글자와 작은 수만 사용합니다.
연습과 같은 문제를 다시 풀어 보는 시간이에요. 힌트 없이 먼저 생각해 보세요. 지금 적은 답은 바로 합격으로 기록되지 않아요.
