Python 오류 질문

YUNY CHOI@Google의 이미지

def word_count(filename):
    f = open(filename)
    counts = {}
    for line in f :
        list = line.split()
        for word in list:
            if word in counts:
                conts[word] += 1
            else:
                counts = 1
    return counts
 
reply = 'y'
while reply == 'y':
    filename = input('파일 이름을 입력해 주세요: ')
    wc = word_count(filename)
 
    for word in sorted(wc):
        print(word, wc[word])
 
    reply = input('계속 하시겠습니까?:')

이런 프로그램을 만들었는데

파일 이름을 입력해 주세요: you
Traceback (most recent call last):
  File "D:\KLDP\질\파일속 단어갯수.py", line 18, in <module>
    wc = word_count(filename)
  File "D:\KLDP\질\파일속 단어갯수.py", line 4, in word_count
    f = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'you'

이렇게 오류가 떴어요. 그래서 이번에 파일 이름에txt를 붙여서 입력했는데

파일 이름을 입력해 주세요: you.txt
Traceback (most recent call last):
  File "D:\KLDP\질\파일속 단어갯수.py", line 18, in <module>
    wc = word_count(filename)
  File "D:\KLDP\질\파일속 단어갯수.py", line 9, in word_count
    if word in counts:
TypeError: argument of type 'int' is not iterable

이런 오류가 생겼어요. 어떻게 수정해야 하는 건가요?

세벌의 이미지

Show your path.

peecky의 이미지

counts가 처음에는 dictionary 였는데, 코드 중간에서 숫자로 바뀝니다.
이 부분을 수정해보세요.

counts = {}
 
...
 
counts = 1