Just a few corrections are needed, read the comments ...
def percount(myFile):
# set period count to zero at start
periods = 0
# loop through each character in the text string
for char in myFile.read():
#print char, periods # testing
if char == '.':
periods += 1
# when done with the loop return period count value
return periods
# create file handle
myFile = open("inn.txt", "r")
p = percount(myFile)
print p
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Line 10 is incorrectly indented inside the for loop.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
I made the corrections I believe but the code still returns 0 even though there are 19 periods in the document. Help?
def percount(myFile):
periods = 0
for char in myFile.read():
if char == '.':
periods +=1
return periods
myFile = open("inn.txt", "r")
p = percount(myFile)
print p
Carefully compare your code with vegaseat's code and the light will come...
@vegaseat: nice to see you again Mr moderator !
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691