Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 114 - 2615_KEC
P. 114

    Output
                       Enter marks: 84
                       Grade A

                      THE NESTED if STATEMENTS

                      Nested if statements mean if statements are                Start
                      placed  within  another if  statement.  Python
                      allows us to  write  an if statement  within
                      another if statement. The syntax of the nested                         False     Statement 3
                      if statement is as follows:                             if condition 1
                      if (conditional expression1):
                                                                                    True
                          statement(s)
                          if (conditional expression2):                                       False
                                                                              if condition 2           Statement 2
                                  statement(s)
                          elif (conditional expression3):
                                  statement(s)                                       True
                          else:                                               Statement 1
                                  statement(s)
                      else:
                                                                                 Stop
                          statement(s)

                      Program 5: To determine which number is greater among three numbers.

                          Program5.py
                       File  Edit  Format    Run   Options   Window    Help

                       #Program to determine which number is greater among three numbers
                       num1 = int(input('Enter the First Number: '))
                       num2 = int(input('Enter the Second Number: '))
                       num3 = int(input('Enter the Third Number: '))
                       if(num1>num2):
                           if(num1>num3):
                               print('First number is the greatest number.')
                           else:
                               print('Third number is the greatest number.')
                       else:
                           if(num2>num3):
                               print('Second number is the greatest number.')
                           else:
                               print('Third number is the greatest number.')

                       112  Premium Edition-VII

| 109 110 111 112 113 114 115 116 117 118 119 |