๐ 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.
Green Light: Move!
Condition:
light == "green"
Boolean Expression:
True
Action: Players can move forward. ๐โโ๏ธ๐จ
light = "green"
if light == "green":
print("Move forward!")
Move forward!
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.
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!
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!
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.
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.
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!
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.
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!
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.
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!
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.
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.
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#
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