97.7k views
5 votes
Write a function, named FileLineCount(), that returns an integer and accepts a string. Pass the function the file name. It should then use a while loop to return the number of lines in a file. Hint: The function will be reading the file you just created above. g

User Shongsu
by
5.3k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

The following code is written in Python. It is a function that accepts a file string and uses that string to read the file in that location. Then it uses a while loop that keeps going until there are no more lines in the file. Each iteration it adds 1 to the count variables. Finally, it prints the count variable which contains the total number of lines in the file. The code has been tested and the output can be seen in the attached image below.

def countLines(file):

file = open(file, 'r')

count = 0

while file.readline():

count += 1

print(str(count) + " total lines")

Write a function, named FileLineCount(), that returns an integer and accepts a string-example-1
User FloT
by
5.1k points