I need a function that finds all the lines that 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'.
It's a miss-indentation:
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'.") # < look at now and before and compare.
Just move the print
to end of the loop.
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'.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.