Skip to content

Step by Step Guide to Building a Conditional (if) Statement

sleepinghungry edited this page Mar 7, 2017 · 3 revisions

Step-by-Step if statement

Step 1: Use the if keyword

if

Step 2: Include a variable

if variable_name

Step 3: Include an operator

Operators include less than (<), greater than (>), equal to (==), not equal to (!=), less than or equal to (<=), and greater than or equal to (>=)

if variable_name < 

Step 4: Include a 2nd value (either another variable or a literal constant (number or string))

if variable_name < 5

Step 5: Include a colon

if variable_name < 5:

Step 6: Press enter to go to the 2nd line

if variable_name < 5:
|    <-- (Pretend this is a blinking cursor)

Step 7: Press tab to indent the 2nd line

if variable_name < 5:
  |  <-- (Pretend this is a blinking cursor)

Step 8: Write the code that should be executed if the above condition were true:

#####Correct if variable_name < 5:
print("Too low!") <-- (Correctly indented line of code) #####Incorrect if variable_name < 5:
print("Too low!") <-- (Incorrectly indented line of code)

Step-by-Step elif statement

Step 9: Add the elif keyword

"elif" is an optional statement. If used, it must be lined up with an "if"

#####Correct if variable_name < 5: print("Too low!") elif <-- (Correct placement of "elif")

#####Incorrect if variable_name < 5: print("Too low!") elif <-- (Incorrect placement of "elif")

Step 10: Add the elif condition

if variable_name < 5:
  print("Too low!")
elif variable_name > 5

Step 11: Add a colon

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:

Step 12: Press enter to go to the next line

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
|  <-- (blinking cursor)

Step 13: Press tab to indent the line

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
  |  <-- (blinking cursor)

Step 14: Enter the code that will run if the elif condition is true

#####Correct if variable_name < 5: print("Too low!") elif variable_name > 5: print("Too high!") <-- (Correctly indented line of code)

#####Incorrect if variable_name < 5: print("Too low!") elif variable_name > 5: print("Too high!") <-- (Incorrectly indented line of code)

Step-by-Step else statement

The "else" statement is optional. If used, the "else" keyword must line up with the associated "if" keyword.

Step 15: Add the else keyword

#####Correct if variable_name < 5: print("Too low!") elif variable_name > 5: print("Too high!") else <-- (Correctly lined up else keyword)

#####Incorrect if variable_name < 5: print("Too low!") elif variable_name > 5: print("Too high!") else <-- (Incorrectly lined up else keyword)

Step 16: Add a colon

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
  print("Too high!")
else:

Step 17: Press enter to move to the next line

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
  print("Too high!")
else:
|  <-- (blinking cursor)

Step 18: Press tab to indent the line of code

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
  print("Too high!")
else:
  |   <-- (blinking cursor)

Step 19: Type in the line of code that should run only if neither of the conditional above it are true

#####Correct

if variable_name < 5:
  print("Too low!")
elif variable_name > 5:
  print("Too high!")
else:
  print("You guessed the secret number!")  <-- (Correctly indented line of code)

#####Incorrect if variable_name < 5: print("Too low!") elif variable_name > 5: print("Too high!") else: print("You guessed the secret number!") <-- (Incorrectly indented line of code)

####Can you include more than one line of indented code after an if, elif, or else? Yes! Any code that is indented after an if, elif, or else will only run if the condition is true. Or in the case of else, only if the other conditions were all false.

if variable_name < 5:
  print("Too low!")
  print("Try a little higher.")
elif variable_name > 5:
  print("Too high!")
  print("Try a little lower.")
else:
  print("You guessed the secret number!")
  print("You are really good at this guessing game!")

Any code that should run outside of the if-statement should be unindented.

if answer == "Salem":
  print("That was the right answer!")

print("This line of code will always run, because it is not indented")

Examples

if answer == "Salem":
  print("That was the right answer!")


if available_guesses > 0:
  print("Try again!)
else:
  print("Sorry, you ran out of guesses!")

BWX Game Engine pages

Python Pages

Step By Step Guides

Building Conditionals

Lectures

Explaining Conditionals

Assignments

I Am Thinking of a Number...

Clone this wiki locally