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

C T  01     Write a program that accepts a number from a user and outputs a message indicating whether
                              it is an even multiple of five. It may be easier to write the code if you recall that Python uses
                              logical operator to combine multiple conditions.




                 Program 10.2 Write a program to check whether a number is even multiple of 5.

                   01 # Objective: To check whether a number is even multiple of 5
                   02 number = int(input("Enter a number : "))
                   03 if number % 2 == 0 and number % 5 == 0:
                   04     print("Number is Even Multiple of 5")
                   05 else:
                   06     print("Number is Not Even Multiple of 5")
                 To combine two or more conditions, we may use logical operators. For example:
                    if num > 0 and num%2 == 0:
                        print("Positive Even Number")
                 If the value of num is greater than 0 and if num is divisible by 2, the conditional expression will evaluate to True.
                 Only then, the statement, print("Positive Even Number")will be executed.
                 Let us now write a program that computes a student's result based on his percentage of marks. Now we are ready
                 to write a function that takes two arguments, the pass percentage (passPercent) and the percentage of marks
                 obtained (percent) by a student, and returns his/her result.
                    def result(passPercent, percent):

                        """
                        Objective: To compute a student's result
                        Inputs:
                        percent : percentage of marks
                        Return value:
                        Student's result (As str object)

                        """
                        if percent >= passPercent:
                            return "Passed"
                        else:
                            return "Failed"
                 In the function result(), considering passPercent  to be 33, if the student has scored 33 or more than 33
                 percent, the conditional expression percent >= passPercent will yield True, and the statement in the if
                 block will get executed. Thus, the function result() will return "Passed". On the contrary, if the student has
                 scored less  than 33 percent, the conditional expression percent >= passPercent will yield False, and the
                 statement in the else block will be executed. So, the function result() will return "Failed".

                 Now, let us write a complete program that does the following four things:
                 1.  Sets pass percentage (passPercent) as 33.
                 2.  Accepts the student's percentage from the user.
                 3.   Invokes  the  function  result  that  accepts  the  pass  percentage  and  the  student's  percentage  of  marks  as
                    arguments and returns the student's result.

                 4.  Prints the student's result as returned by the function result.

                                                                                               Conditional Statements  259
   268   269   270   271   272   273   274   275   276   277   278