15. Selection with IF

  • IF is the first conditional statement you will use
  • It allows you to execute different code depending on the circumstances
  • Look how the code is indented to identify it is part of the IF statement
#Declare variables
letter = "a"

#Check if letter is 'a'. If so, print message
if letter == "a":
    print("The letter is a")
    print("Indented lines are included!")

#Print this message anyway because there is no indent
print("This prints no matter what happens")

Tasks

  1. Type in the code above and make sure it works
  2. Modify the program to accept a character typed in by the user
  3. Modify the program to check for the letter ‘b’
  4. Save and label your code

Extension Task

If statements work with numbers too. Create an IF statement to check a number.