Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In python please use advanced functions (map,lambda etc) if possible When the end of the semester is near, most students want to calculate their CGPA.
In python please use advanced functions (map,lambda etc) if possible
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 function which has the name and parameters: get_spa (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(6799123, 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_pa() raises a KeyError. In this case, you don't need to call get_spa() anymore, and your function calculate_cgpa) should simply retum -1.8. If the student was on an exchange program in another university for the semester, get_gpa) will raise an IndexError. In this case you should assume that the GPA of the student for that semester is 3.e 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 valueError 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 3.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: o summing his/her GPA's for 5 available semesters and o adding 3.0 for the semester he/she was on exchange, and o 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 Index Error for the exchange semester and 2 valueError 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 OUT 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. student may be on exchange and on leave for multiple semesters. You should not try to implement get_spa(). 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. Click here to use Python Interpreter to debug your code. Sample I/O: >>> calculate_cgpa(5678901) 2.74 Reset 1 from get_spa inport get_spa 3 I DO NOT CHANGE ABOVE 4 HAU WRITE YOUR CODE UNDER THIS LINEStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started