Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON3.X ( please comment code as well) In this program you will be writing dat

ID: 3867254 • Letter: P

Question

PYTHON3.X ( please comment code as well) In this program you will be writing data to and reading data from a file.  A good way to start to this assignment is to begin by getting the output written to the screen. That is if a student record is supposed to look like "Dave, 35, 25, 99" then begin by seeing if you can get that output written to the monitor. All parts must be in the same python file. Use a main() function and other functions that you create as needed.

Part A:

Input names and three test scores of students from the user, terminated by "ZZZ" and create a data file "grades.txt" with records of the following form:

student_name (String), test_1 (Integer), test_2 (Integer), test_3 (Integer)

Note: the terminating student name (ZZZ) should not appear in the data file

Part B:

Read and Display the contents of the file "grades" created in Part a. Each student’s record should appear on a separate line and include the total score (the sum of the three tests) for that student.

See the sample python interaction to see how the data should look at output.

Create functions that will work with the following main(). That is, your main() should contain the following code:

Part C: put a time-stamp at the end of the output

Sample Python interaction (Bold Italics is input)

Sample Text File containing the output from the program:

Explanation / Answer

#!usr/python/bin

from datetime import datetime

def getStudentData(fn,scores):
    name = ""
   
    fout = open(fn,'w')
    name = input("Please enter the student names(ZZZ to finish):")
    while name != "ZZZ":
        data = name
        for i in range(scores):
            score = input("Enter Score {}:".format(i+1))
            data = data + " " + score    
        fout.write(data+" ")
        name = input("Please enter the student names(ZZZ to finish):")
    fout.close()   

def getTextFileContents(fn):
    fin = open(fn,'r')
    lines = fin.readlines()
    return lines

def printReport(lines):
    print("-----------------------------------------------------")
    print("Student Name Score1   Score2   Score3 Total")
    print("-----------------------------------------------------")
    for l in lines:
        total = int(l.split(' ')[1]) + int(l.split(' ')[2]) + int(l.split(' ')[3])
        print(l.split(' ')[0] + "            " + l.split(' ')[1] +"   " + l.split(' ')[2] + "   " + l.split(' ')[3][:-1] + "   " + str(total))
       
    print("-----------------------------------------------------")
    weekday = datetime.now().strftime('%a');
    month = datetime.now().strftime('%B')
    month = month[0:3]
    now = datetime.now()
    print(weekday + " " + month + " " + str(now.day) + " " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + " " + str(now.year))

  

def main():
    # set the working file name
    fn = "grades.txt"

    # set the number of scores
    scores = 3
   
    # get student names and scores, persisting them into a file
    getStudentData(fn, scores)
   
    # obtain the student data from the file
    lines = getTextFileContents(fn)

    # print the file contents in report format
    printReport(lines)
   

if __name__ == "__main__":
    main()