The Python code for the program that can analyze the students' data and print the appropriate grade reports is below:
def calculate_gpa(grades):
total_grade_points = sum(grade * credit_hours for grade, credit_hours in grades)
total_credit_hours = sum(credit_hours for credit_hours in grades)
return total_grade_points / total_credit_hours
def print_grade_report(student):
if student["isTuitionPaid"] == "Yes":
gpa = calculate_gpa(student["grades"])
print(f"Student Name: {student['studentName']}")
print(f"Student ID: {student['studentID']}")
print(f"GPA: {gpa:.2f}")
print("-----------------------------------")
for course in student["grades"]:
print(f"Course Name: {course['courseName']}")
print(f"Course Number: {course['courseNumber']}")
print(f"Credit Hours: {course['creditHours']}")
print(f"Grade: {course['grade']}")
print("-----------------------------------")
else:
print(f"Student Name: {student['studentName']}")
print(f"Student ID: {student['studentID']}")
print("Grades withheld for nonpayment of tuition.")
print("Billing Amount: ${student['billingAmount']:.2f}")
print("-----------------------------------")
def main():
# Read the input file
with open("input.txt", "r") as input_file:
lines = input_file.readlines()
# Extract the tuition rate per credit hour
tuition_rate = float(lines[0].split()[1])
# Process each student's data
for student_data in lines[2:]:
student = {}
for item in student_data.split():
if item == "isTuitionPaid":
student["isTuitionPaid"] = item
elif item == "numberOfCourses":
student["numberOfCourses"] = int(item)
else:
if "studentName" not in student:
student["studentName"] = item
elif "studentID" not in student:
student["studentID"] = item
else:
course = {}
course["courseName"] = item
student["grades"].append(course)
if item == "grade":
student["grades"][-1]["grade"] = item
student["billingAmount"] =TuitionRate * sum(course["creditHours"] for course in student["grades"])
print_grade_report(student)
if __name__ == "__main__":
main()
What is the code?
The above said program will read information about students from a file, figure out how much each student needs to pay for school, and then create a report card for each student.
Therefore, The report will show the student's name, ID number, GPA (if they paid for claasses), and their grades in all their courses. If the student hasn't paid their tuition, the grade report will show a message saying that the grades are being held and how much the student needs to pay.