Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When the end of the semester is near, most students want to calculate their CGPA. We have their GPA's for 8 semesters in a database,

When the end of the semester is near, most students want to calculate their CGPA. We have their GPA's for 8 semesters in a database, so we will try to help them. However, while requesting the GPA's from the database, we may face some errors.

In this task, you will write a function named calculate_cgpa() which takes the ID of a single student as an integer and returns the student's CGPA as a float, rounded to 2 decimals(see the note below).

To get the GPA of a student for a semester, a function which has the name and parameters: get_gpa(student_id, semester_no) is provided to you. This function is implemented in a different file and already imported in your template code. Therefore, you will be able to directly use it. This function takes two arguments:

  1. student_id (as an integer), and
  2. semester_no (an integer in the range of [1,8])

and returns the GPA of the student (between 0.0 and 4.0) as a float. For example, get_gpa(6790123, 3) returns the GPA of the student with ID 67890123 for the 3rd semester. For a single student, you should call get_gpa() for each semester (i.e. 8 times) to get his/her GPA's for 8 different semesters.

However, this function may raise errors:

  • If the student's ID does not exist in the database, the function get_gpa() raises a KeyError. In this case, you don't need to call get_gpa() anymore, and your function calculate_cgpa() should simply return -1.0.
  • If the student was on an exchange program in another university for the semester, get_gpa() will raise an ValueError. In this case you should assume that the GPA of the student for that semester is 1.0 and use this value in your calculations.
  • If the student was on leave for a semester, than he/she will not have a GPA for that semester. If this is the case, the function get_gpa() will raise a IndexError and you should skip that semester and continue your calculation with other semesters.

While calculating the CGPA of a student, you should sum GPA's for all available semesters, add 1.0 for exchange semesters (if there are any) and divide the total by the number of semesters the student was not on leave. For example:

  • A student's CGPA who was not on leave or not on exchange for any semester is calculated by summing his/her GPA's for all 8 semesters and dividing the sum by 8.
  • A student's CGPA who was on leave for 2 semesters and on exchange for another semester is calculated by:

    • summing his/her GPA's for 5 available semesters and
    • adding 1.0 for the semester he/she was on exchange, and
    • dividing the total by the number of semesters the student was not on leave (6).

In the second example, get_gpa()would raise 3 errors: 1 ValueError for the exchange semester and 2 IndexError for on leave semesters.

IMPORTANT NOTE: Just before returning the CGPA of the student, you must round it to 2 decimal places by using the built-in round() function. An example rounding looks like this: round(calculated_cgpa, 2) # returns calculated_cgpa, rounded to 2 decimal places

Tips and Clarifications

  • Since you do not have the get_gpa() function, you can not test your code on other editors (e.g. PyCharm, Google Colab). Test your code on class3 or on our Python Interpreter, by clicking the link below. (Please do not import get_gpa while testing on our Python Interpreter, we did it for you.)
  • If you need to test your code, here are some of the ID's that exist in the database: 2171544, 2315477, 2424937
  • In your calculate_cgpa() function, you should handle the errors raised by get_gpa().
  • When get_gpa() raises an error, it does not return any values
  • "On exchange" and "on leave" are different things.
  • A student may be on exchange and on leave for multiple semesters.
  • You should not try to implement get_gpa(). It is already implemented. You will only call it where you need it.
  • You should not call your calculate_cgpa() function anywhere, we will call it ourselves while evaluating your code.
  • You should not print anything.
  • You should not take any input.

input: >>> calculate_cgpa(5678901)

output: 2.08

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

Students also viewed these Databases questions

Question

What is Accounting?

Answered: 1 week ago

Question

Define organisation chart

Answered: 1 week ago

Question

What are the advantages of planning ?

Answered: 1 week ago