Page 75 - Touhpad Ai
P. 75

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)
                   # Output the final amount after applying the discount
                   print(f"Total Amount: Rs. {total_purchase_amount:.2f}")
                   print(f"Discount Rate: {discount_rate * 100}%")
                   print(f"Discounted Amount: Rs.{discounted_amount:.2f}")
                   Output:

                   Enter the total purchase amount: Rs. 5500
                   Congratulations! You get a 5% discount.
                   Total Amount: Rs. 5500.00
                   Discount Rate: 5.0%
                   Discounted Amount: Rs. 5225.00





                                                                                 Basic Concepts of Artificial Intelligence  73
   70   71   72   73   74   75   76   77   78   79   80