Assignment: 1) I want to create a getData.py python program and implement it in
ID: 3740006 • Letter: A
Question
Assignment:
1) I want to create a getData.py python program and implement it in main.py. I have the code done but I am unable to figure out how to take a chunk of code that I have in the main.py code format it as the getData.py code and then simply implement back into main.py since it is a requirement. Can I get help with that? I need it within a few hours.
2) Also I need help with format the decimal places (round to two decimal places) for the GPA in the final output. Your help will be much appreciated. Thanks. Also refer to this question for additional doubts on the missing files - https://www.chegg.com/homework-help/questions-and-answers/assignment-description-student-object-consists-firstname-string-lastname-string-birthdate--q27904363
main.py code
Explanation / Answer
# getData.py
import os.path
from datetime import date
studentList = []
gradesList = []
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):
totalCredits = 0
totalG = 0
for record in self.grades:
if record[1] == 'A':
totalG = totalG + 4 * record[2]
elif record[1] == 'B':
totalG = totalG + 3 * record[2]
elif record[1] == 'C':
totalG = totalG + 2 * record[2]
else:
totalG = totalG + 1 * record[2]
totalCredits = totalCredits + record[2]
if totalCredits == 0:
return str(0) + '(' + str(totalCredits) + ' credits)'
return str(totalG / totalCredits) + '(' + 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 : %.2f' % (self.currentGPA()) # decimal upto 2 places
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 = wordSplits[1]
grade = classID[4]
credits = int(classID[5])
classID = classID[:4]
gradesList.append((techID, classID, grade, credits))
def readFromFile():
readFromFileTwo(inputFileTwoName)
readFromFileOne(inputFileOneName)
# end of getData.py
# end of main.py
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.