Your program should calculate and output (to the screen) the following information about the file of text:
1. The total number of lines in the file, including blank lines.
2. The number of blank lines in the file.
3. The number of periods in the file.
4. The number of characters in the file which are neither blanks nor newline characters, including all punctuation. (Be careful of red herrings…)
5. The number of words in the file. Note: there will not be any “cheap-tricks” regarding this in the input file. Words will not be hyphenated over two lines.

Hi all, just a little confused on how to approach parts of this program. In the part that I have written so far, I can calculate the number of lines and the number of blank lines. When I hit part 3, I get a little confused. Currently, "periods" always returns as 0. Here is what I have so far:

lines = 0
blanklines = 0
periods = 0

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

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

for char in myFile:
    if char == '.':
        periods +=1

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", periods

Note that we are not allowed to access built in python string functions besides len.

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

When you get to "for char in myFile" you are already at the end of the file (because "for line in myFile" went through the entire file) so there is no data read. It should be:

myFile = open("inn.txt", "r")
 
for line in myFile:
    lines +=1
 
    if line == ('\n'):
        blanklines +=1
 
    for char in line: 
        if char == period:             #3
        if char not in [" ", "\n"]:    #4 do both in the same loop
    #etc...#5 will also be under the "for line in myFile" loop and use "line".

Python does have a count() function but I assume that you are supposed to code this for yourself.

Edited 5 Years Ago by woooee: n/a

blanklines = periods = 0

with open("advsh12.txt") as my_file:
    for linecount, line in enumerate(my_file, 1):   
        if line == ('\n'):
            blanklines +=1
        elif '.' in line:
            periods += line.count('.')

    for item, count in ('lines', linecount), ('blank lines', blanklines), ('periods', periods):
        print "The total number of %s in the file is %i" % (item, count)

For number 4 check split and join methods from your materials or use count method (do count of newlines and blanks and reduce from total length).

Edited 5 Years Ago by pyTony: n/a

Edit: pyTony, we have not covered "with" and "as" in class as of yet, and we are not able to use the .count.

I used woooee's technique and switched the names. However, upon reading the lab more carefully, the period counting must be done in a function. This is what I have so far, please forgive me for being inexperience as I am still grasping concepts of Python. Currently, this function always returns 1. Also, we must write a function that calculates the total number of characters and punctuation, excluding blank spaces and newline characters.

def percount(myFile):
    periods = 0
    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:
        if char =='.':
            numper = percount(myFile)

Edited 5 Years Ago by bigredaltoid: n/a

You are passing myFile instead of numper to percount, also parameter name is written incorrectly and it is allways set to 1 in lines 2 and 3, simple return periods+1 would have been enough. I would think that the teacher expects the whole for loop of 16-19 to be in the function.

I must not be understanding something correctly. If I declare periods = 0 in the function, it always returns 0. If I declare it outside of the function, it says it is called before being defined and I get an error.

def percount(line):
    for char in line:
        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

If someone can help me with this period function, I can figure out the other function.

By the way we can't use the string.split, as it is a string module. She says we cannot use anything string module related.

I am stuck on the period too, because there is a nice catch at the end of the directions.

It has to be able to count the number of periods even if there is more than one per line!

Edited 5 Years Ago by NateN34: n/a

Your percount looks ok, just use it instead of count method of line.

I looked little again your code and noticed that ypu are not initializing the counting variable.

Your percount looks ok, just use it instead of count method of line.

I looked little again your code and noticed that you are not initializing the counting variable.

This question has already been answered. Start a new discussion instead.