16.9k views
0 votes
Grade Report: The mid-semester point at your local university is approaching. The registrar’s office wants to prepare the grade reports as soon as the students’ grades are recorded. However, some of the students enrolled have not yet paid their tuition. 1. If a student has paid the tuition, the grades are shown on the grade report together with the grade point average (GPA). 2. If a student has not paid the tuition, the grades are not printed. For these students, the grade report contains a message indicating that the grades have been held for nonpayment of the tuition. The grade report also shows the billing amount. The registrar’s office and the business office want your help in writing a program that can analyze the students’ data and print the appropriate grade reports. The data is stored in a file in the following form: 15000 345 studentName studentID isTuitionPaid numberOfCourses courseName courseNumber creditHours grade courseName courseNumber creditHours grade . . . studentName studentID isTuitionPaid numberOfCourses courseName courseNumber creditHours grade courseName courseNumber creditHours grade . . . The first line indicates the number of students enrolled and the tuition rate per credit hour. The students’ data is given thereafter. A sample input is as follows: 3 345

1 Answer

3 votes

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.

User Chydik
by
7.3k points