Day 2: Conditionals

back · home · slides · CMSC 201 (Fall 2024) @ UMBC · Making decisions based on variables!

CMSC 201: Conditionals P1

Agenda:

  • HW0 retrospective
  • HW1 overview
  • Fun computer culture knowledge: open source software
  • Who cares? (AKA: what will we make)
  • Conditionals
  • Advanced conditionals preview
  • HW0

    Went pretty OK I think :)

    Reminder: Do not submit HW through blackboard for this class, it will not be looked at. (You can submit something if it makes you feel good! But it will still not be looked at). TAs will grade sometime between soon and soonish.

    HW1 is much more in depth and worth more points! Some of you have already started, that's great!

    Looking at HW1: input, printing, and variables

    HW1

    Open source software

    We know the difference between compiled vs interpreted programs: compiled are very hard to read (without special tools).

    If you wrote a program and distributed just the binary, it would be very difficult for people to understand or change!

    You could easily:

    Open source software or free and open source software (FOSS) is code that is licensed to assure the following freedoms:

    (from gnu.org): A program is free software if the program's users have the four essential freedoms: [1]
    • The freedom to run the program as you wish, for any purpose (freedom 0).
    • The freedom to study how the program works, and change it so it does your computing as you wish (freedom 1). Access to the source code is a precondition for this.
    • The freedom to redistribute copies so you can help others (freedom 2).
    • The freedom to distribute copies of your modified versions to others (freedom 3). By doing this you can give the whole community a chance to benefit from your changes. Access to the source code is a precondition for this.

    Common memes:

    Who cares?

    Let's continue with the game theme :) Let's make a dialogue tree! We'll use input, variables, and a bit of conditionals. Ours will be text only, naturally :)

    Conditionals

    Basic idea: IF this, then THAT. And sometimes, IF this, then THAT, otherwise THIS.

    if temperature > 70:
        print("summer clothes!")
    if temperature > 70:
        print("summer clothes!")
    else:
        print("maybe wear a jacket")

    The if statement

    if condition:
        print("if body")
    print("rest of program")

    condition can be anything that evaluates to a boolean (or Truthy/Falsy...) value. Such as:

    Now you see the power of conditionals. We can make decisions on ANYTHING!

    print("welcome!")
    if input() == "hello":
        print("hello!")
    print("how can I help you?")

    The else statement

    if condition:
        print("if body")
    else:
        print("else body")
    print("rest of program")

    The elif statement

    if condition:
        print("if body")
    elif condition:
        print("elif body")
    else:
        print("else body")
    print("rest of program")
    if condition:
        print("if body")
    elif condition:
        print("elif body")
    elif condition:
        print("elif body")
    elif condition:
        print("elif body")
    else:
        print("else body")
    print("rest of program")

    else and elif are used for mutually exclusive conditions. Use them when you only want ONE path to execute.

    These two programs are different. How?

    power_level = int(input())
    if power_level < 10:
        print("your power level is too low; I am impervious to attack!")
    if power_level % 2 == 0:
        print("your power level is even! I am too odd; I am impervious to attack!")
    else:
        print("oh no!")
    
    power_level = int(input())
    if power_level < 10:
        print("your power level is too low; I am impervious to attack!")
    elif power_level % 2 == 0:
        print("your power level is even! I am too odd; I am impervious to attack!")
    else:
        print("oh no!")
    

    The latter one uses elif, making each if "body" mutually exclusive (meaning only one of them will run).

    Truthy/Falsey values

    Python tries to be helpful by having things that are not booleans still evaluate to true or false.

    The None type

    Python has a special "type" or object: None. It's the closest thing Python has to a NULL or empty value. It represents a value of nothingness. It is always considered false (False).

    Preview of next class's material, to prime the mind, and help demonstrate what we learned!

    Combining conditions

    Python keywords and and or let us combine multiple conditionals into one statement.

    if temperature > 70 and ac_broken == True:
        print("summer clothes!")

    We can group these with parentheses. Normally they are evaluated left to right.

    True and False
    True and (False or True)

    Short circuiting

    When the outcome of a condition with multiple parts is obvious, Python won't bother evaulating unnecessary parts.

    Why are these two code blocks different?

    temperature = 60
    if temperature > 70 and ac_broken == True:
        print("summer clothes!")
    temperature = 80
    if temperature > 70 and ac_broken == True:
        print("summer clothes!")

    Cursed example: Combining our type lore knowledge, what would this print?

    if print("condition one") and print("condition two"):
        print("wow!")

    Review: Operators

    Operation Examples
    Adding 2 + 3 x += 4 "hi how are" + " you"
    Multiplying 100 * 4 y *= z "a" * 50
    Subtraction 10 - 7 x -= 41
    Division 70 / 7 x /= 5
    Integer division 71 // 7 y //= 5
    Modulus 80 % 7 y %= 10

    Let's write some code! Dialogue tree time :)

    Here's what I'm imagining:

    # shopkeeper.py --> cool shopkeeper interaction
    item = ""
    discount = 1
    currency = 15
    
    ## interact : determines discount
    print("Shopkeep: Welcome to walmart!")
    user_greeting = input("User: ")
    
    if user_greeting == "beanz":
        discount = 0.5
    elif user_greeting == "Soupz":
        discount = 0.7
    
    ## buy item
    print("Shopkeep: What do you want?")
    print("TP - $5")
    print("Milk - $3")
    user_item = input("User: ")
    
    if user_item == "TP":
        price_after_discount = 5 * discount
        currency -= price_after_discount
        item = "TP"
        print("Cool!")
    elif user_item == "Milk":
        price_after_discount = 3 * discount
        currency -= price_after_discount
        item = "Milk"
        print("Milky!")
    else:
        print("Why'd you come to walmart?")
    
    ## print summary
    print("Item:", item)
    print("Discount:", discount)
    print("Currency:", currency)