Page 74 - Touhpad Ai
P. 74

else:
                     print("It's not freezing.")
                 Output:

                 Please enter the temperature in Celsius: 35
                 It's not freezing.
              The if-elif-else Statement
              The if…elif…else  statement  helps  us  to  test
              multiple  conditions  and follows  a top-down
              approach.  The if statement  is  used  to  check  a   Test Expression 1  True  Statement Block 1
              single  condition, while the  elif (short for "else
              if") statement  allows you  to  check  additional
              conditions  if  the  previous  conditions  are  not      False
              met. If none of the conditions evaluates to True,
              then the final else statement gets executed. The     Test Expression 2  True  Statement Block 2
              if…elif…else  statement  provides  a way to  handle
              multiple decision branches in your code.
                                                                       False
              The syntax of the if...elif...else statement is as follows:
                 if (Test Expressions_1):                                       True
                                                               Test Expression 3       Statement Block 3
                          Statement block 1
                 elif (Test Expression_2):

                          Statement block 2                            False           Statement Block 4
                 elif (Test Expression_3):
                          Statement block 3
                                                                                                Execute statement outside
                 else:                                                                              if...elif...else block
                          Statement block 4
                 Program 16: To demonstrate the use of the if-elif-else statement

                 # Asking the user for input
                 score = int( input("Please enter your exam score: "))
                 # Grading the score
                 if score >= 90:
                     print("Your grade is A.")
                 elif score >= 80:
                     print("Your grade is B.")
                 elif score >= 70:

                     print("Your grade is C.")
                 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.




                 72     Touchpad Artificial Intelligence - XI
   69   70   71   72   73   74   75   76   77   78   79