can anyone tell me what is wrong with my code?
def count_letters(in_file, out_file):
in_file = open("in_file.txt", "r")
for line in in_file:
for x in sorted(line):
count = x.count()
out_file = open("out_file.txt", "w")
print(x, " ", count)
in_file.close()
out_file.close()
print(out_file)
it's supposed to
- Takes two filenames (in_file and out_file)as arguments
- Opens and reads the input file specified by in_file, and counts the number of occurrences of each letter (in a case-insensitive manner)
- Writes the result in the output file specified by out_file
when I run it, it tells me that "in_file" is not defined, but I have defined it, and made an "in_file.txt."
any help would be appreciated!
in_filebut then re-defining it inside the function. You might want to reconsider that.out_file = open("out_file.txt", "w")out of theforloops. And you never write to the output file.