Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 129 - 2610_Springdales_C-7
P. 129

    The syntax of nested if statement is shown below:
                                                                                   Start
                     Syntax:

                     if (Test Expression1):
                                                                                                False
                          if (Test Expression2):                               if condition 1              block 3
                               Indented block 1
                          else:                                                       True
                               Indented block 2
                                                                                                False
                     else:                                                      if condition 2             block 2
                          Indented block 3

                     Program 5: To find out if the student is selected for             True
                     the college or not based on the given conditions –            block 1
                     1.  Selected if age > 18 and marks > 65

                     2.  Not selected if age > 18 and marks < 65
                                                                                   Stop
                     3.  Not selected if age < 18

                         Program5.py
                      File  Edit  Format   Run   Options   Window    Help

                      #Program to check if a student is selected for college or not
                      name = input('Enter the name of the student: ')
                      age = int(input('Enter the age of the student: '))
                      marks = int(input('Enter the marks of the student: '))
                      if(age > 18):
                          if(marks > 65):
                              print(name, 'is selected for college')
                          else:
                              print(name, 'is not selected for college')
                      else:
                          print(name, 'is too young for college')

                     On running the program given on previous page, we get the following output:

                         Output

                      Enter the name of the student: Amit
                      Enter the age of the student: 18
                      Enter the marks of the student: 57
                      Amit is too young for college

                      Enter the name of the student: Kanak
                      Enter the age of the student: 23
                      Enter the marks of the student: 87
                      Kanak is selected for college

                                                                                    Conditional  Statements in Python  127

| 124 125 126 127 128 129 130 131 132 133 134 |