Page 275 - Computer Science V2.0 Class 11
P. 275
Next, let us use Program 10.4 to compute simple interest for a sum of 4000 rupees for time periods of six years and two years:
>>> Enter Principal Amount: 4000
>>> Enter time period in years: 6
Simple Interest= 960.0
>>> Enter Principal Amount: 4000
>>> Enter time period in years: 2
Simple Interest= 240.0
Program 10.5 Write a function grade(percentage) that computes the student's grade based on the student's
percentage and the cut-off marks specified for that grade.
percentage grade
>=75 A
>=60 B
>=50 C
>=33 D
< 33 F
The function grade() only needs to check in sequence whether a student's percentage exceeds or equals the
percentage required for the grade A, B, C, D. If a student has scored a percentage of marks that does not qualify
him/her for even 'D' grade, he/she is awarded an 'F' grade, indicating that the student has failed. Note that the
high level of nesting makes the function grade() somewhat hard to read.
Note that in the function grade(), an if-statement is enclosed within another if-statement. When a statement
encloses another statement, the enclosed statement is called a nested statement. The function grade() illustrates that
the nested statement can again enclose another statement, and so on. Next, let us make use of the function grade()
in Program 10.5 to display a student's grade.
01 '''
02 Objective: To display a student's grade, based on his/her percentage
03 User input: percentage of marks obtained by the student
04 Output: Student's grade: A, B, C, D, or F
05 '''
06 #Approach: First write a function to compute student's grade
07 def grade(percentage):
08 '''
09 Objective: To compute a student's grade, based on his/her percentage
10 Input: percentage: percentage of marks obtained by the student
11 Return Value: grade (A, B, C, D, F) cutoffs 75, 60, 50, 33, respectively
12 '''
13 cutOffA = 75
14 cutOffB = 60
15 cutOffC = 50
16 cutOffD = 33
17 if percentage >= cutOffA:
18 return 'A'
19 else:
20 if percentage >= cutOffB:
21 return 'B'
22 else:
23 if percentage >= cutOffC:
24 return 'C'
25 else:
26 if percentage >= cutOffD:
27 return 'D'
28 else:
29 return 'F'
Conditional Statements 261

