Skip to content

Latest commit

 

History

History
177 lines (177 loc) · 6.92 KB

File metadata and controls

177 lines (177 loc) · 6.92 KB

< Previous       Next >


If Statements

An if statement runs a block of code based on whether or not a condition is true.
1. Create two variables:
a = 6
b = 3
2. Add an if statement:
if a > b:
  print("a is greater than b") 
3. Run your program.
4. Switch the values of a and b , then run the program again.


Did anything happen?
If statements only run if the condition is true, so the above statement only runs when a is greater than b.

Indentation

In Python, indentation is how code relationships are shown. Anything indented from the line above it is part of the same statement.
Try typing:
x = 2 
print(x) 
if x > 3:
  x += 2
print(x)
Run the above code. You should see 2 printed to the screen.
Now, try running the code again, but with the second print not indented.
x = 2 
if x > 3:
  x += 2
print(x)


Indenting print(x) includes it with the if statement above.

Boolean Values

Remember that a boolean value is either true or false.
Add the following line to your file:
has_key = True
Add an if statement to see if the boolean value becomes true, and if so, tell the player they've won:
if has_key == True:
  print("You won! Unlock the door!")
Tip: Always try to write boolean values so they read as true in plain English. For instance, setting lost == True would read as: "if lost:"

Relational Operators

You can use the following symbols, called relational operators, to compare two values in an if statement:
  • < - Less than. For example, a<b will return true if a is less than b.
  • > - Greater than. For example, a>b will return true if a is greater than b.
  • <= - Less than or equal to. For example, a<=b will return true if a is less than or equal to b.
  • >= - Greater than or equal to. For example, a>=b will return true if a is greater than or equal to b.
  • == - Equals. For example, a==b will return true if a is equal to b.
  • and - And means both statements must be true. For example, (a>b) and (b<c) will return true of both a>b and b<c returns true.
  • or - Or means either statement can be true. For example, (a>b) and (b<c) will return true of either a>b and b<c returns true.
  • not - Not returns true if the statement is not true. For example, not a will return true if a is false.
You can also print out true/false checks with a print statement! Guess what the output will be before you run the statement.
  • print(5 > 4) checks if 5 is greater than 4.
  • print(10 == 10) checks if 10 is equal to 10.
  • print(5>10) checks if 5 is less than 10.
  • print(not 4>5) checks if 4 is not greater than 5.
  • print(5>4 and has_key == True)
  • print(4>5 or 4>6) checks if either 4 is greater than 5 or 4 is greater that 6.

Elif and Else Practice

If statements pair up with elif (short for "else if") and else statements to perform a series of checks.
A full if/elif/else block is in the code box below.
Copy this code into your file.
player_age = 12
if player_age >= 18: print("You could be in college.") elif player_age >= 13: print("You can also attend iD Academies!") elif player_age >= 7: print("You can attend iD Tech Camps!") else: print("You're young.")
Run your code and input multiple ages to see which part of the if/elif/else block prints out.

Specific Checks

If and elif statements always look for a specific condition:
if player_age > 18: #Happens if the age is greater than 18.
elif player_age > 13: #Only happens if the age is not greater than 18 and greater than 13.
An else statement doesn't look for a specific condition. Else statements occur after an if or elif statement in your code.
if player_age > 18: #Happens if the age is greater than 18.
else: #Would happen for any other age not specified above.
Every if - elif - else block must begin with one regular if statement, and end with a single else statement, but you can have as many elif statements in the middle as you want!

Comparison Keywords

What if you want to check a couple of things at once? Use and to check if two conditions are true.
if x > 2 and x < 4:  #Does something only if both cases are true.
Copy the code below:
player_has_item = False
score = 50
won = False
if player_has_item and score > 100: won = True
if not won: print("You haven't beaten the game yet.") elif won: print("You won the game!")
Use or to check if either condition is true. If either one is true, the if statement will run its code.
Copy the code below:
player_has_item = False
score = 100
won = False
if player_has_item or score > 200: won = True
if not won: print("You haven't beaten the game yet.") elif won: print("You won the game!")
Change the code so it prints out the "You won the game!" statement. This time, you only need to change one of the variables to win this time.

Not

Another keyword, not, checks if something is false.
if not won: #Does something if this case is not true.
This statement is commonly used in games when you want the game to keep running if it isn't won.
Below is a number guessing program that has been modified to not run properly.
Copy the code below:
import random
computer_number = random.randrange(0, 101)
guessed = False
while True: if guessed: answer = input("Guess the number ") if int(answer) == computer_number: guessed = True print("You got it!") break elif int(answer) > computer_number: print("Your guess is too high.") else: print("Your guess is too low.")
else: break
Update the code to run by adding the not keyword to the proper if statement!

Recap

You can use relational symbols in conditional statements to see if two things are greater than, less than, etc. in relation to each other. Use and to check two conditions at once, or to check one of two conditions, and not to check if a condition is false.
Examples:
  • if a == b - checks if a is equal to b
  • if a <= b - checks if a is less than or equal to b
  • if a > b - checks if a is greater than b