Using python to program the folowwing problem. A student object consists of: Fir
ID: 3808248 • Letter: U
Question
Using python to program the folowwing problem.
A student object consists of:
FirstName: string
LastName: string
Birthdate: tuple with (month, day, year), such as (1,17,1998)
TechID: string
Grades: list of tuples (each tuple will have a 4-digit course ID, course grade, and number of credits) such as [(3127,”C”,4),(2496,”B”,3),(6703,”D”,3)]
So one student might look like this:
FirstName: “Bob”
LastName: “Smith”
Birthdate: (2,17,1998)
TechID: “43546578”
Grades: [(5930,”B”,4),(7660,”C”,4),(3122,”C”,3),(5438,”C”,2),
(6730,”C”,4),(4268,”C”,4),(5709,”D”,4),(8071,”B”,4)]
Create a list of students. The data for each student can be found in the text files.
Your program should read the data from each of the files, as appropriate. (Do not change the format of the input files.)
Create three methods for a student object:
currentAge: How old is the student? Be sure to use the Python module. Be sure that you do not just subtract the birth year from the current year.
Hint:
import datetime
now = datetime.datetime.now()
print (now.year)
currentGPA: What is the student’s GPA?
Report: Add the output for that student to the output file.
Each output file item should look like this, for the above student:
Bob Smith (#43546578)
Age: 18 (02/17/1998)
GPA: 2.14 (29 credits)
We use two input files fir is formatted as follows:
87965164,Paris,Yu,6/27/1997
87965219,Heath,Moss,10/13/1996
The number at the beginning is a techID and you use it to find the classes taken by the student from the second input file:
87965219,5569A4
87965158,5668D3
After the techID it is the class number, the grade and the credits its worth.
Finally we write the output listed above to an output file.
Explanation / Answer
#!/usr/bin/python
import os.path
from datetime import date
studentList = [];
gradesList = [];
outputFileName = "output.txt";
inputFileOneName = "inputOne.txt";
inputFileTwoName = "inputTwo.txt";
class Student:
def __init__(self, firstName, lastName, birthDate, techID, grades):
self.firstName = firstName;
self.lastName = lastName;
self.birthDate = birthDate;
self.techID = techID;
self.grades = grades;
def currentAge(self):
birthDay = self.birthDate[0]
birthMonth = self.birthDate[1]
birthYear = self.birthDate[2]
today = date.today()
return today.year - birthYear - ((today.month, today.day) < (birthMonth, birthDay))
def currentGPA(self):
# I don't know how to calculte GPA from grages
# Give the formula, I will update the code accordingly
totalCredits = 0;
for record in self.grades:
totalCredits = totalCredits + record[2];
return "2.5 (" + str(totalCredits) + " credits)";
def report(self):
name = self.firstName + " " + self.lastName + "(#"+ str(self.techID) +")";
doB = str(self.birthDate[0]) +"/" + str(self.birthDate[1]) + "/" + str(self.birthDate[2]);
age = "Age : " + str(self.currentAge()) + " (" + doB +")";
gpa = "GPA : " + str(self.currentGPA());
return (" " + name + " " + age +" " +gpa +" ");
def readFromFileOne(fileName):
doesFileExists = False;
if os.path.isfile(fileName) :
doesFileExists = True;
# If the file exists then Load the existing data into the studentList
if doesFileExists :
fileInstance = open(fileName,'r');
fileText = fileInstance.read();
lineSplits = fileText.split(" ");
for line in lineSplits :
if line == "" :
continue;
wordSplits = line.split(",")
techID = int(wordSplits[0]);
firstName = wordSplits[1];
lastName = wordSplits[2];
doBString = wordSplits[3];
dobSplits = doBString.split("/");
dobTuple = (int(dobSplits[0]), int(dobSplits[1]), int(dobSplits[2]));
studentGrades = []
for record in gradesList:
if record[0] == techID:
studentGrades.append((record[1], record[2], record[3]));
student = Student(firstName, lastName, dobTuple, techID, studentGrades);
studentList.append(student);
def readFromFileTwo(fileName):
doesFileExists = False;
if os.path.isfile(fileName) :
doesFileExists = True;
# If the file exists then Load the existing data into the studentList
if doesFileExists :
fileInstance = open(fileName,'r');
fileText = fileInstance.read();
lineSplits = fileText.split(" ");
for line in lineSplits :
if line == "" :
continue;
wordSplits = line.split(",")
techID = int(wordSplits[0]);
classID = int(wordSplits[1]);
grade = wordSplits[2];
credits = int(wordSplits[3]);
gradesList.append((techID, classID, grade, credits));
def readFromFile():
readFromFileTwo(inputFileTwoName);
readFromFileOne(inputFileOneName);
# Starting the procedure
readFromFile();
fileInstance = open(outputFileName,'w');
for student in studentList:
ouputString = student.report();
fileInstance.write(ouputString);
fileInstance.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.