About the program:

The coding is basic to intermediate level; using several of the python coding features taught in various tutorials such as how to open a text file, use functions, print statements, while loops and If-Else statements.  One of the really interesting features of this program is being able to input a person’s first name and have it go through an existing text file and find a matching name along with its corresponding line items.  The text file is created using Notepad or WordPad, and to make it simple is placed in the C: directory under a folder named “data”.  The program will thus open the text file...  c:/data/Directory.txt

''' Program starts here '''

with open('c:/data/Directory.txt', 'r') as f:
        data = f.readlines( )

global name
name = ""
print("")
name = input_data(name)
print("")
process(name)

 At the start of the program, the statement opens the text file and reads all the lines of text into the variable called ‘data’.

with open('c:/data/Directory.txt', 'r') as f:
        data = f.readlines( )

Also note that the statement used to open the text file called Directory.txt is preferably used since one will not need to worry about closing the file later on.  

Then you will notice ‘global name’.  This is used to help prevent the input data stored in ‘name’ from later overriding new desired input data stored to 'name'.  Since the program uses the variable ‘name’ both in and out of a few functions, the statement ‘global name” was also used within those functions.  The function input_data(name) is then used with the argument ‘name’.   The result from the function input_name(name) is then passed on to the variable also called ‘name’  --.>    name = input_data(name)

The function definition for input( ) is towards the top of the program and is written as follows:

def input_data(name):
      print("")
      name = input("      Enter First Name: ")    
      print("")
      return name

  Then the function process(name) is called using the input variable 'name' as the argument.   The function  definition is also found towards the top of the program code.

def process(name):            # set to zero in order to read the header line first

            x = 0
        while x != len(data):
            report(data,x)
            x += 0
        else:
            print("")

    The next part uses a while loop that will go through an iteration starting with x = 0 and stops when ‘x’  either reaches the end of the file (i.e. one less than the length of the text file) or if a match has been found.  Note that len(data) will contain the total number of lines within the text file.  Also, one can either use != or < within the while statement yielding the same results.  And it does not matter how many lines of data are included within the text file so long as the iteration of  ‘x’ ends up being one less than the length of the file so as to avoid getting a blank output (a no match).

Note: It is helpful to recognize that using certain embedded print statements within the code can often help to track or locate problems that occur when first building or testing the program.  For example, one can include the following print statement in order to follow the iterations of ‘x’ and the total number of lines being looked at as the function called report(name) is called repeatedly in order to locate the matching first name within the file.  

x = 1
    while x != len(data):
        print(str(x) + " ...

Read more »