# Conditions Week 4 NOTES: New rule: closed laptops _unless_ we are doing an exercise. --- ## Review  <!-- .element: style="height:400px" --> -- What is a variable? A named container for data <!-- .element: class="fragment green" --> -- What are the five basic data types in Python? - Integer <!-- .element: class="fragment green" --> - Floating-point number <!-- .element: class="fragment green" --> - String <!-- .element: class="fragment green" --> - Boolean <!-- .element: class="fragment green" --> - NoneType <!-- .element: class="fragment green" --> -- What data type would you use for a price? Floating-point number <!-- .element: class="fragment green" --> -- What data type would you use for a name? String <!-- .element: class="fragment green" --> -- What data type would you use for a true/false fact? Boolean <!-- .element: class="fragment green" --> -- What does `print()` do? outputs text to the terminal <!-- .element: class="fragment green" --> -- What type of data does `input()` always return? String <!-- .element: class="fragment green" --> -- How do you convert a string to an integer? `int()` <!-- .element: class="fragment green" --> ```py [2] age_string = input("How old are you? ") age_integer = int(age_string) ``` <!-- .element: class="fragment" --> -- How can you add to a string? Concatenation (or f-string) <!-- .element: class="fragment green" --> ```py [3] first_name = "Rylan" last_name = "Schubkegel" full_name = first_name + " " + last_name ``` <!-- .element: class="fragment" --> -- What is an f-string? A string that can insert variables <!-- .element: class="fragment green" --> ```py [2] name = input("Enter your name: ") print(f"Hello, {name}") ``` <!-- .element: class="fragment" --> -- What are the two boolean values? `True` and `False` <!-- .element: class="fragment green" --> NOTE: Booleans are a primitive type from week 2. Today they become useful. --- ## Making Decisions <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- So far, programs run **top to bottom** Every line happens, every time <!-- .element: class="fragment" --> -- Real programs **choose** what to do - If the password is correct, log in <!-- .element: class="fragment" --> - If the answer is right, add a point <!-- .element: class="fragment" --> - If you are 18 or older, you can vote <!-- .element: class="fragment" --> -- A **condition** is a yes/no question The computer's answers are `True` and `False` <!-- .element: class="fragment" --> -- ```py if True: print("Pinocchio's nose will grow") ``` --- ## Comparisons <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- | Operator | Meaning | | --- | --- | | `==` | equal to | | `!=` | not equal to | -- `=` **assigns** a value `==` **compares** two values <!-- .element: class="fragment" --> -- Comparison operators always result in a `True`/`False` value -- What is the output of each line? ```py x = 2 print(x == 2) # 1 print(x == 3) # 2 print(x != 3) # 3 ``` 1. True <!-- .element: class="fragment green" --> 2. False <!-- .element: class="fragment green" --> 3. True <!-- .element: class="fragment green" --> -- | Operator | Meaning | | --- | --- | | `<` | less than | | `>` | greater than | | `<=` | less than or equal to | | `>=` | greater than or equal to | -- What is the output of each line? ```py print(10 > 3) # 1 print(2 < 2) # 2 print(7 >= 7) # 3 print(4 <= 3) # 4 ``` 1. True <!-- .element: class="fragment green" --> 2. False <!-- .element: class="fragment green" --> 2. True <!-- .element: class="fragment green" --> 3. False <!-- .element: class="fragment green" --> -- What's wrong here? ```py age = 18 can_vote = age = 18 print("You are 18 years old:", can_vote) ``` `=` assigns, it does not compare <!-- .element: class="fragment green" --> --- ## The `if` Statement <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- Three parts: 1. `if` 2. A condition that is `True` or `False` 3. A **colon** `:` then an indented block ```py [|2|3] age = int(input("Enter your age: ")) if age >= 18: print("You can vote!") ``` -- Python indentation ```py if True: print('1 space is fine') if True: print('8 spaces is fine') if True: print() print('4 then 5 spaces is NOT FINE') ``` NOTE: indentation is one of the most common problems I see -- If the condition is `True`, run the indented lines If the condition is `False`, skip them -- What's wrong here? ```py if age >= 18 print("You can vote.") ``` Missing colon `:` <!-- .element: class="fragment" --> --- ## `else` and `elif` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- `else` (the other path) ```py [5-6] age = int(input("Enter your age: ")) if age >= 18: print("You can vote.") else: print("You cannot vote yet.") ``` Exactly one of these two messages will print <!-- .element: class="fragment" --> -- `elif` (else if) Check another condition when the first one was `False` -- ```py [5-6] age = int(input("Enter your age: ")) if age < 5: price = 5 elif age < 16: price = 10 else: price = 18 print(f"You'll pay ${price} for the ticket") ``` NOTE: - Conditions are checked **in order** - The first `True` one wins; the rest are skipped - `else` is optional, but useful as a fallback -- Order matters! ```py score = 95 if score >= 60: print("Pass") elif score >= 90: print("A") ``` What prints? <!-- .element: class="fragment" --> `Pass` — the first condition is already `True` <!-- .element: class="fragment" --> NOTE: Put the most specific checks first. For grades, check A before Pass. -- Separate `if`s can **all** run `elif` is only tried if the previous check failed ```py score = 95 if score >= 90: print("A") if score >= 60: print("Pass") ``` ```txt A Pass ``` <!-- .element: class="fragment" --> --- ## Checking Answers <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- ```py answer = input("What is the capital of France? ") if answer == "Paris": print("Correct!") else: print("Incorrect.") ``` -- `"Paris"` and `"paris"` are **not** equal Tell the user the expected format <!-- .element: class="fragment" --> NOTE: This is in the quiz assignment. You can also convert the answer with `.lower()` if you want to accept any capitalization. -- Optional: ignoring capitalization ```py answer = input("What is the capital of France? ") if answer.lower() == "paris": print("Correct!") else: print("Incorrect.") ``` -- Example: keeping score ```py score = 0 answer = input("2 + 2 = ") if answer == "4": print("Correct!") score = score + 1 else: print("Incorrect.") print(f"Your score: {score}") ``` NOTE: `score += 1` means "add 1 to score." Same as `score = score + 1`. This is exactly what the quiz assignment needs. -- ### Exercise Make a "guess the number" game! 1. Pick a secret number (e.g., 7) 2. Ask the user to guess the number 3. If the guess is correct, print "You got it!" Otherwise, print "Nice try." ---  <!-- .element: style="height:400px" -->