Page 278 - Computer Science V2.0 Class 11
P. 278

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, base d on his/her percentage
                10     Input: percentage: percentage of marks obtained by t he student
                11     Return Value: grade: A, B, C, D, F
                12     cutoffs 75, 60, 50, 33, respectively
                13     '''
                14     cutOffA = 75
                15     cutOffB = 60
                16     cutOffC = 50
                17     cutOffD = 33
                18     if percentage >= cutOffA:
                19         return 'A'
                20     elif percentage >= cutOffB:
                21         return 'B'
                22     elif percentage >= cutOffC:
                23         return 'C'
                24     elif percentage >= cutOffD:
                25         return 'D'
                26     else :
                27         return 'F'
                28 percentage = int(input("Enter student's percentage: "))
                29 yourGrade = grade(percentage)
                30 print('Your grade is : ', yourGrade)
              In the example given above, if percentage  is 54, each of the  the conditional expressions percentage  >=
              cutOffA,  and  percentage  >=  cutOffB  yields (on evaluation) False,  but the conditional expression
              percentage >= cutOffC yields (on evaluation) True, so the statement:

                  return 'C'
              is executed and the value of yourGrade is set equal to 'C'. Finally, on the execution of the statement:

                  print('Your grade is', yourGrade)
              Python interpreter  outputs the message:
              Your grade is C

              Sample Output:
               >>> Enter student's percentage:  54
                    Your grade is C

               Program 10.7 Write a program that checks whether a number is positive, negative, or zero, and displays an appropriate
               message.

              Solution:
                01 #Objective: To check whether a number is positive, negative or zero
                02 num = int(input("Enter a number : "))
                03 if num > 0:
                04     print("POSITIVE")
                05 elif num < 0:
                06     print("NEGATIVE")
                07 else:
                08     print("Number is ZERO")


               264   Touchpad Computer Science (Ver. 2.0)-XI
   273   274   275   276   277   278   279   280   281   282   283