Page 193 - AI Ver 3.0 Class 11
P. 193

elif score >= 60:
                         print("Your grade is D.")

                     else:
                         print("Your grade is F. You need to retake the exam.")
                 Output:
                     Please enter your exam score: 85
                     Your grade is B.

                 Nested If Statement
                 A nested if statement is a construct in programming where an if statement is placed within another if statement’s
                 block. This allows for the evaluation of multiple conditions in a hierarchical manner. In Python, indentation is crucial for
                 defining the scope of each if statement.
                 The syntax of nested if statement is shown below:

                     if (Test Expression1):
                          if (Test Expression2):
                               Indented block 1

                          else:
                               Indented block 2
                     else:
                          Indented block 3

                  Program 17: To demonstrate the use of the nested if statement
                     # Input the total purchase amount
                     total_purchase_amount = float(input("Enter the total purchase amount: Rs. "))



                     # Initialize the discount rate and discounted amount
                     discount_rate = 0
                     discounted_amount = 0


                     # Calculate the discount rate and discounted amount based on the total purchase
                     amount
                     if total_purchase_amount >= 10000:
                         discount_rate = 0.10  # 10% discount
                         print(f"Congratulations! You get a 10% discount.")
                     else:
                         if total_purchase_amount >= 5000:

                             discount_rate = 0.05  # 5% discount
                             print(f"Congratulations! You get a 5% discount.")
                         else:
                           print("You are not eligible for getting discount!!")
                     # Calculate the discounted amount
                     discounted_amount = total_purchase_amount - (total_purchase_amount * discount_rate)



                                                                                         Python Programming     191
   188   189   190   191   192   193   194   195   196   197   198