dimanche 25 août 2019

Getting Row Count With for-loop

I will need a function to find all the lines which end with "end". Then, I need to count how many lines all together and use print to print out the number.

This is my code below:

  count = 0

  for line in open("jane_eyre.txt"):
      line_strip = line.rstrip()
      if line_strip.endswith(" end"):    
         lines = line_strip
         count += 1
         print("There are", count, "lines that end in 'end'.")  

Expected Result:

There are 4 lines that end in 'end'.

My Current Result:

There are 1 lines that end in 'end'.

There are 2 lines that end in 'end'.

There are 3 lines that end in 'end'.

There are 4 lines that end in 'end'.

This looks like a very newbie question and really thank you if you can point out my mistake here!

Aucun commentaire:

Enregistrer un commentaire