Write a correct, well-structured, well-documented Python program to create a list of lists containing student information.  You are to create one master list whose elements must be lists containing elements for the student’s first name, last name, major, credit hours completed, and quality points earned.  (The organization of the master list will be similar to what you did in Lab 11.)  The data must be read from a text file named “prog3.txt” in the default directory.  Each line in this file contains the data for one student.  Individual items of data on a line are separated by one or more spaces.  The order of the data items on a line is guaranteed to be techid, first name, last name, major, credits, quality points.

Once your master list is created, you should print it to demonstrate that it was created correctly.  Then your program must iterate through the list and ask the user for end-of-the-semester updates.  This will entail asking the number of credits completed and the number of quality points earned in this semester for each of your students.  These numbers are then to be added to the values stored in the list and the updated values are to be stored back in the list.

Once all of the updates are complete, print the master list again to demonstrate that you did the updates correctly.

Finally, “disassemble” your master list into lines of text of the same format as those you read in and write them, line by line, to a text file called “out.txt” in the default directory.

Your program must also meet the following design specifications.
•   You are allowed to use any string functions or methods that you wish.
•   You must put the code for your main program in a function named main.
•   You program must read its initial data from the file named prog3.txt in the default directory
•   You must have a function which has a single parameter, representing a line of text from the file.  This function must the list of the information for that student.
•   Once the above function returns the list for a single student, you must then incorporate this list into your master list.
•   The update information must be entered from the keyboard and must be only the numbers for the current semester.  (Do not ask the user for the total number of credits or quality points.)  These input values must be added to the previously stored numbers and the totals must be stored back in the appropriate entries of your data structure.  You should also display enough information about the student so the user knows which person is being asked about.
•   The final output file must be called out.txt in the default directory and must be formatted in the same manner as the original input file.


This is the code i have so far which I can get the master list to print, having trouble with the adding and printing a new list. Please Help.


    emptyList = []

    def studentInf(file):

        file.readline()

        for line in file:

            line = line.strip()

            line = line.split(' ')

            emptyList.append(line)

        return emptyList


    newList = []

    def newList(emptyList) :

        for line in emptyList:
            techId = (line[2])
            firstName = (line[0])
            lastName = (line[1])
            credit = int(line[3])
            qPoint = int(line[4])
        reutrn(newList)


    def main():


        file = open('Lab11.txt', 'rU')

        emptyList = studentInf(file)

        newList(emptyList)

        print

        file.close

    main()



This program will print out a GPA for each Student using an input file. I am using this as a base to my program because the first function will create a list Which is my master list. What do I have to change in the next two functions to make it take each student 1 at a time from file and add more credit hours and quality points then create a new list with the updated credit hours and quality points. I also want all this to print to an outfile.

Recommended Answers

All 3 Replies

Code tags are better when just the code is tagged. Still, better than no tags at all.

I don't see how the code you posted does anything at all. The lists are instantiated outside of the functions and in the case of newList(), not even used. Let's see what might work...

From your lines 44-48, I'm figuring you have a file where each line after the header is: first name space last name space id space credit space, grade point. OK. To get that into your master list, let's take your file reading program and fix it up a little:

def studentInf(file):
        emptyList=[]
        file.readline()
        for line in file:
            line = line.strip()
            line = line.split()
            emptyList.append(line)
        return emptyList

Note that emptyList is now initialized inside the function. BTW, I don't know why you call this "emptyList" as it has whatever data is in the file.

Now you could print it line by line:

for i in emptyList: print "ID: {2} - name: {0} {1}, credits: {3}, grade point {4}".format(i)

Now, you're supposed to go through that list and update information for each student. This would be something like:

for i in len(emptyList):
    emptyList[i]=updateRec(emptyList[i])

where you need a function to update the record. Note that what your doing here is updating the elements of "emptyList", not creating a new list. UpdateRec() will take your sub-list and ask the user for new data:

def updateRec(lstA):
  lstA[3]=int(raw_input("new credit for " + lstA[0] + lstA[1] + ": "))
  lstA[4]=int(raw_input("new grade point average for " + lstA[0] + lstA[1] + ": "))
  return lstA

or something like that.

I entered in the update Rec function and a error pops up

def studentsFile(inFile):
    emptyList = []
    for line in inFile:
        line = line.strip()

        line = line.split(' ')

        emptyList.append(line)

    return emptyList


def main():
    inFile = open("Lab11.txt", "r")
    emptyList = studentsFile(inFile)
    print(emptyList)

main()

This is what I have as of right Now I need another function that will add a number entered by a user to a specific person in the master list, then print and updated list

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.