Hi..me again. I am trying to incorporate this function which counts the periods into the code..however, I can not get the code to output both the line count, blank line count, and period count at the same time. This is my 3rd or 4th try at adding this into the code in different positions. What happens right now is a valueerror, in previous spots lines were returning 0 or periods was returning 0.

def percount(myFile):
    periods = 0
    for char in myFile.read():
        if char == '.':
            periods +=1
    return periods


lines = 0
blanklines = 0



myFile = open("inn.txt", "r")


        
for line in myFile:
    lines +=1
    
    if line == ('\n'):
        blanklines +=1
      
    for char in line:
        p = percount(myFile)
    


print "The total number of lines in the file is",lines
print "The total number blank lines in the file is", blanklines
print "The total number of periods in the file is", p

You should be passing line to the function.

def percount(line):
    periods = 0
    for char in line:
        if char == '.':
            periods +=1
    return periods

for line in myFile:
...
##    for char in line:
    p = percount(line)
    total_periods += p

Also, in the real world
if line == ('\n'):
is rarely used as there may a space in the line as well. You can use instead:

if len(line.strip()):
commented: Wooee has been an immense amount of help to this new Python user! +1

Thank you for the quick response, worked like a charm. And also thanks for that additional piece of info.

Vegaseats' code did not work here because you are already reading the file with
"for line in myFile".
Since you already have a individual line, it is better to use that line than read the file all over again.

I'll just reply here in order to stop wasting thread space. I am now trying to count the amount of words in the document. I think I am on the right track..my Professor used an example wuth currch = line[0] it says line is not defined..I know I need to update the previous char to the current ch before updating the current char, but I am not sure how to approach it.

def percount(line):
    periods = 0
    for char in line:
        if char == '.':
            periods +=1
    return periods


def charcount(line):
    charnum = 0
    for char in line:
        if char not in [" ", "\n"]:
            charnum +=1
    return charnum
    
lines = 0
blanklines = 0
total_periods = 0
total_char = 0
prevCh = ""
currCh = line[0]
seperators = 0


myFile = open("inn.txt", "r")
        
for line in myFile:
    lines +=1
    
    if line == ('\n'):
        blanklines +=1

    p = percount(line)
    total_periods += p

    c = charcount(line)
    total_char += c

    for word in line:
        if currCh == " " and prevCh != " ":
            seperators +=1
    words = seperators + 1
if len(line.strip()):
•	You may NOT use any of the functions or methods from the “string” module.

Normally you would use line.strip().split() and the length of the resulting list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.