You have a couple problems.
First, when reading a file you should specify the mode (not strictly needed, but greeatly helps clarify intent). In this case for reading do:
open("file_x.txt", "r")
Next, when reading a file you need to make sure you close it after you're done. You should use with for this:
with open("file_x.txt", "r") as in_file:
lines = in_file.readlines()
You didn't loop the lines, you looped the already read file in_file. Change to:
for line in lines:
You didn't use a string to check the line, you used the variable d which is an int 0. Change to "d"
if "d" in line:
All together now:
with open("file_x.txt", "r") as in_file:
lines = in_file.readlines()
d = 0
for line in lines:
if "d" in line:
d += 1
print(d)
Another error. If you want to count all occurrences and not just how many lines contain the letter, you'll want to use str.count. Also you can avoid calling readline if you directly loop over the file:
d = 0
with open("file_x.txt", "r") as in_file:
for line in in_file:
d += line.count("d")
print(d)
Going a bit further with sum and a generator expression:
with open("file_x.txt", "r") as in_file:
d = sum(line.count("d") for line in in_file)
print(d)
if d in line:heredis your int variable you have declared above. use'd'.in_file.for line in in_file: d+=line.count('d')for line in in_file: for i in line: if i=='d': d+=1