I need to write a program that opens and reads a file and contains separate user-defined functions for counting the number of lines and words in the file e.g. linecount(), wordcount()etc. I drafted the below code, but I keep getting a name error that says "global name 'f' is not defined". f is a file handle that should be returned by the openfile() function. Any advise?
#open and read file
def openfile():
import string
name = raw_input ("enter file name: ")
f = open (name)
# Calculates the number of paragraphs within the file
def linecount():
openfile()
lines = 0
for line in f:
lines = lines + 1
return lines
#call function that counts lines
linecount()