Page 134 - TP_Pluse_V2.2_Class_7
P. 134

THE if…elif…else LADDER
                                                                           Start
                  The  if...elif...else ladder  is  another  type of if
                  statement. It helps us to test multiple conditions
                  and follows  a top-down  approach.  In  this, as      if condition 1  True     block 1
                  soon as the condition of the if evaluates to true,
                  the indented  block  associated  with that  ‘if’ is          False
                  executed, and the rest of the ladder is avoided.
                  If none of the conditions evaluates to true, then     if condition 2  True     block 2
                  the final else statement gets executed.

                  The syntax of if...elif...else ladder is shown below:        False

                  Syntax:
                                                                                        True
                  if (Test Expressions_1):                              if condition 3           block 3
                      Indented block 1
                                                                              False               block
                  elif (Test Expression_2):

                      Indented block 2
                                                                                                             Stop
                  elif (Test Expression_3):

                      Indented block 3

                  else:

                      Indented block

                   Program  7: To  check  whether  a  given  number  is  two-digit  number,  three-digit  number  or
                   four-digit number.



                      Program7.py
                   File  Edit  Format   Run    Options   Window    Help


                   num = int(input('Enter a number: '))
                   if(9 < num <= 99):
                       print('Two digit number')
                   elif(99 < num <= 999):
                       print('Three digit number')
                   elif(999 < num <= 9999):
                       print('Four digit number')
                   else:
                       print('The number is not in the two-digit, three-digit, or four-digit
                   range.')






                  132   Plus (Ver. 2.2)-VII
   129   130   131   132   133   134   135   136   137   138   139