๐Ÿ“– Boolean: Squid Game Red Light, Green Light Edition ๐Ÿฆ‘๐Ÿšฅ#

Syntax

If statements in Python have the following syntax:

if condition:
    # code block
elif condition:
    # code block
else:
    # code block

The elif and else blocks are optional. You can have as many elif blocks as you want.

The condition is an expression that evaluates to True or False.

The code block is indented by tab or four spaces.


  1. Green Light: Move!

  • Condition: light == "green"

  • Boolean Expression: True

  • Action: Players can move forward. ๐Ÿƒโ€โ™‚๏ธ๐Ÿ’จ

light = "green"

if light == "green":
    print("Move forward!")
Move forward!
  1. Red Light: Stop!

  • Condition: light == "red"

  • Boolean Expression: True

  • Action: Players must freeze or face elimination. โŒ

light = "red"

if light == "red":
    print("Freeze! Don't move.")
Freeze! Don't move.
  1. Caught Moving

  • Condition: light == "red" and player_moving == True

  • Boolean Expression: True

  • Action: Player is eliminated. ๐Ÿ˜ฑ

player_moving = True

if light == "red" and player_moving:
    print("Youโ€™re eliminated!")
Youโ€™re eliminated!
  1. Safe Zone Reached

  • Condition: player_position >= finish_line

  • Boolean Expression: True

  • Action: Player wins the round! ๐Ÿ†

player_position = 6
finish_line = 5

if player_position >= finish_line:
    print("You made it to the finish line!")
You made it to the finish line!
  1. Timeโ€™s Up

  • Condition: time_remaining <= 0 and player_position < finish_line

  • Boolean Expression: True

  • Action: Player is eliminated for not finishing in time. โณโŒ

time_remaining = -1
player_position = 4

if time_remaining <= 0 and player_position < finish_line:
    print("Out of time! Youโ€™re eliminated.")
Out of time! Youโ€™re eliminated.
  1. Strategic Slow Play

  • Condition: light == "green" and player_speed < max_speed

  • Boolean Expression: True

  • Action: Move cautiously to avoid detection. ๐Ÿข

player_speed = 10
max_speed = 20
light = "green"

if light == "green" and player_speed < max_speed:
    print("Move carefully.")
Move carefully.
  1. Player Distraction

  • Condition: player_focus == False

  • Boolean Expression: True

  • Action: Risk of moving at the wrong moment increases. ๐Ÿ˜ต

player_focus = False

if not player_focus:
    print("Stay focused or you might move on red!")
Stay focused or you might move on red!
  1. Game Over

  • Condition: all_players_eliminated == True

  • Boolean Expression: True

  • Action: The game ends, and the prize remains unclaimed. ๐Ÿ’€

all_players_eliminated = True

if all_players_eliminated:
    print("Game over. No winners.")
Game over. No winners.
  1. Light Status Check

Using if-else to handle both red and green lights in one block

  • Condition: light == "red" or light == "green"

  • Boolean Expression: True

  • Action: Players must stop or move based on the light status. ๐Ÿšฆ

light = "green"

if light == "green":
    print("Move forward quickly!")
else:
    print("Freeze immediately!")
Move forward quickly!
  1. Player Progress Check

Using if-else to track player progress

  • Condition: player_position < finish_line

  • Boolean Expression: True

  • Action: Player must keep moving to reach the finish line. ๐Ÿƒโ€โ™‚๏ธ

player_position = 3
finish_line = 5

if player_position >= finish_line:
    print("Victory! You've survived the game!")
else:
    print(f"Keep moving! Still {finish_line - player_position} steps to go.")
Keep moving! Still 2 steps to go.
  1. Survival Status

Using if-elif-else to handle multiple game states

  • Condition: player_alive == True

  • Boolean Expression: True

  • Action: Player can keep playing the game. ๐ŸŽฎ

  • Condition: player_alive == False

  • Boolean Expression: True

  • Action: Player has been eliminated from the game. ๐Ÿ’€

  • Condition: all_players_eliminated == True

  • Boolean Expression: True

  • Action: The game ends, and the prize remains unclaimed. ๐Ÿ’€

player_alive = True
reached_finish = False

if not player_alive:
    print("Game Over - You've been eliminated!")
elif reached_finish:
    print("Congratulations - You've won the prize!")
else:
    print("Stay alive and keep moving!")
Stay alive and keep moving!
  1. Movement Speed Check Using if-elif-else to determine playerโ€™s movement strategy

player_speed = 15
max_speed = 20
min_speed = 5

if player_speed > max_speed:
    print("Too fast! You'll have trouble stopping on red light!")
elif player_speed < min_speed:
    print("Too slow! You'll run out of time!")
else:
    print("Good pace! Keep this speed.")
Good pace! Keep this speed.
  1. Game Status Update Using if-elif-else to show game progress

players_remaining = 100
total_players = 456
eliminated_players = total_players - players_remaining

if eliminated_players == total_players:
    print("Game Over - No survivors!")
elif eliminated_players == 0:
    print("Game Just Started - All players still in!")
else:
    print(f"Game In Progress - {players_remaining} players remaining.")
Game In Progress - 100 players remaining.
  1. Risk Assessment Using nested if-else for complex game situations

distance_to_finish = 10
time_remaining = 30
light = "green"

if light == "green":
    if distance_to_finish > time_remaining:
        print("Warning: You might not make it in time!")
    else:
        print("You're on track to finish!")
else:
    if player_moving:
        print("Eliminated - Movement detected on red light!")
    else:
        print("Good - Standing still on red light.")
You're on track to finish!

Red Light, Green Light: Understanding == vs is in Python ๐Ÿšฆ#

๐ŸŸฅ == (Equality): Red Light#

  • Checks value equality.

  • Compares if two objects have the same value, regardless of whether they are the same object in memory.

  • Use it when you care about whatโ€™s inside the objects being compared.

๐Ÿ“– Example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)  # True, because their values are equal
True

๐ŸŸฉ is (Identity): Green Light#

  • Checks object identity.

  • Compares if two objects are the same object in memory.

  • Use it when you care about whether theyโ€™re literally the same object.

๐Ÿ“– Example:

a = [1, 2, 3]
b = [1, 2, 3]

print(a is b)  # False, because they are different objects in memory
False

๐Ÿšจ Common Pitfall: Strings and Small Integers#

Python sometimes optimizes memory by reusing objects for immutable types like strings and small integers. This can lead to surprising results with is.

๐Ÿ“– Example:

x = "hello"
y = "hello"

print(x is y)  # True, because of string interning
True

Butโ€ฆ

x = [1, 2, 3]
y = [1, 2, 3]

print(x is y)  # False, because lists are mutable and stored separately
False

TL;DR ๐Ÿ“#

  • Use == for comparing values.

  • Use is for comparing identity (memory location).

  • If in doubt, think: โ€œAm I checking the content or the container?โ€

โšก Pro Tip: In Python, mutable objects like lists, dicts, and sets behave differently from immutable ones like strings, tuples, and numbers when it comes to identity! ๐Ÿง 

Bonus: Game Masterโ€™s Control#

  1. Switch Light

  • Condition: game_master_switch == True

  • Boolean Expression: True

  • Action: The light toggles between red and green. ๐ŸŽ›๏ธ

game_master_switch = True

if game_master_switch:
    light = "red" if light == "green" else "green"

print(light)
red