Computer Science
Grade 8
20 min
Conditional Logic: Using 'if' Statements for Story Branches
Students will use 'if' statements to create different story branches based on player choices.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define conditional logic and its role in programming.
Explain the purpose and basic syntax of 'if', 'elif', and 'else' statements in Python.
Write Python code that uses 'if' statements to create simple decision points in a story.
Design and implement multiple story branches based on user input using conditional logic.
Identify and debug common errors in conditional 'if' statement logic.
Apply comparison and logical operators within conditional statements to create more complex decision-making.
Articulate how conditional logic is fundamental to creating interactive games and applications.
Ever wished you could choose your own adventure in a game or story? 🎮 What if your choices could change the entire ou...
2
Key Concepts & Vocabulary
TermDefinitionExample
Conditional LogicThe ability of a program to make decisions and execute different blocks of code based on whether certain conditions are true or false.If it's raining, take an umbrella. Else, leave it at home.
'if' StatementA fundamental control flow statement that allows a program to execute a block of code only if a specified condition evaluates to True.if score > 100: print('You win!')
'else' StatementUsed in conjunction with an 'if' statement, the 'else' block executes its code only if the 'if' condition (and any preceding 'elif' conditions) evaluates to False.if age >= 18: print('Adult') else: print('Minor')
'elif' StatementShort for 'else if'...
3
Core Syntax & Patterns
Basic 'if' Statement Syntax
if condition:
# code to execute if condition is True
The 'if' keyword is followed by a condition (a Boolean expression) and a colon. The code block to be executed must be indented (typically 4 spaces) below the 'if' line. If the condition is True, the indented code runs; otherwise, it's skipped.
'if-else' Statement Syntax
if condition:
# code for True
else:
# code for False
This structure provides two alternative paths. If the 'if' condition is True, its block runs. If it's False, the 'else' block runs. Only one of the two blocks will ever execute.
'if-elif-else' Chain Syntax
if condition1:
# code for condition1 True
elif condition2:
# code fo...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You are designing a game where a player can disarm a trap only if they have the 'toolkit' item AND the trap is NOT 'magical'. Given the variables below, which `if` statement correctly represents the condition to succeed?
has_toolkit = True
is_magical = False
A.if has_toolkit == True or is_magical == False:
B.if has_toolkit == True and is_magical == True:
C.if has_toolkit == False and is_magical == False:
D.if has_toolkit == True and is_magical == False:
Challenging
Consider this code for giving a player a reward. Why is this structure of separate `if` statements flawed for awarding a single, tiered reward?
score = 95
if score > 50:
reward = "Bronze Medal"
if score > 75:
reward = "Silver Medal"
if score > 90:
reward = "Gold Medal"
A.The code is fine; a player with 95 points will correctly get the 'Gold Medal'.
B.player with a score of 95 will have their `reward` set to 'Bronze', then overwritten to 'Silver', then finally overwritten to 'Gold', which is inefficient but works. The real bug is that a player with 60 points gets no reward.
C.The code will crash because a variable, `reward`, is assigned multiple times.
D.This structure is flawed because all three `if` conditions are true for a score of 95. The `reward` variable will be assigned three times, and the final value will be 'Gold Medal'. An `if-elif-else` structure with ordered checks (from highest to lowest) would be more correct and efficient.
Challenging
In a complex story, the player wins if they have the 'ancient_amulet', OR if they have both the 'silver_sword' AND their 'power_level' is over 9000. Which single conditional statement correctly implements this logic?
A.if has_ancient_amulet or (has_silver_sword and power_level > 9000):
B.if has_ancient_amulet and (has_silver_sword or power_level > 9000):
C.if (has_ancient_amulet or has_silver_sword) and power_level > 9000:
D.if has_ancient_amulet and has_silver_sword or power_level > 9000:
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free