-
Notifications
You must be signed in to change notification settings - Fork 1
Step by Step Guide to Building a Conditional (if) Statement
if
if variable_name
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 <
if variable_name < 5
if variable_name < 5:
if variable_name < 5:
| <-- (Pretend this is a blinking cursor)
if variable_name < 5:
| <-- (Pretend this is a blinking cursor)
#####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)
"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")
if variable_name < 5:
print("Too low!")
elif variable_name > 5
if variable_name < 5:
print("Too low!")
elif variable_name > 5:
if variable_name < 5:
print("Too low!")
elif variable_name > 5:
| <-- (blinking cursor)
if variable_name < 5:
print("Too low!")
elif variable_name > 5:
| <-- (blinking cursor)
#####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)
The "else" statement is optional. If used, the "else" keyword must line up with the associated "if" 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)
if variable_name < 5:
print("Too low!")
elif variable_name > 5:
print("Too high!")
else:
if variable_name < 5:
print("Too low!")
elif variable_name > 5:
print("Too high!")
else:
| <-- (blinking cursor)
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!")
if answer == "Salem":
print("That was the right answer!")
print("This line of code will always run, because it is not indented")
if answer == "Salem":
print("That was the right answer!")
if available_guesses > 0:
print("Try again!)
else:
print("Sorry, you ran out of guesses!")