강의

멘토링

커뮤니티

Inflearn Community Q&A

joysohee07093584's profile image
joysohee07093584

asked

Free Python Tutorial (Basic) - Become a Developer in 6 Hours

Quiz #7

이렇게 풀면 왜 오류가 날까요?

Written on

·

347

1

import pickle
num = 1
while True:
    num += 1
    with open(str({num})+"주차.txt".format(num),"w"encoding="utf8") \
        as numb_file:
    numb_file.write("- " + str({num}) + \
        "주차 주간보고 - \n부서 :\n이름 :\n업무 요약: ")
    if num == 50:
        break
python

Answer 2

0

with open(str({num})+"주차.txt".format(num),"w", encoding="utf8") as numb_file:

문장에서 .format이 왜 필요한걸까요? 그리고 str({num})을 굳이{num}으로 하는 이유가 있을까요? 그냥 str(num)로 하면 안되는 걸까요?

0

nadocoding님의 프로필 이미지
nadocoding
Instructor

안녕하세요

numb_file.write 앞에 indent 즉 띄어쓰기 4칸이 들어가야 with 에 포함된 동작으로 정상 수행됩니다.

아래 코드 참고하세요 ^^ 다른 부분도 조금 바꿨고 주석으로 설명을 추가했어요

import pickle
num = 0 # 1부터 시작하면 num += 1 에 의해 파일이름이 2부터 시작되므로 0으로 수정
while True:
    num += 1
    with open(str({num})+"주차.txt".format(num),"w", encoding="utf8") as numb_file:
        numb_file.write("- " + str({num}) + "주차 주간보고 - \n부서 :\n이름 :\n업무 요약: ") # indent 추가
    if num == 50:
        break
joysohee07093584's profile image
joysohee07093584

asked

Ask a question