Computer Science
Grade 8
20 min
User Input and Decision-Making: Implementing Choices
Students will learn to handle user input (e.g., keyboard presses) to allow players to make choices in the story.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Use the `input()` function to gather information from a user.
Construct `if`, `elif`, and `else` statements to create decision points in a program.
Apply comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) to evaluate user choices.
Convert user input to different data types (e.g., string to integer) when necessary.
Design and implement simple branching narratives based on user decisions.
Combine user input and conditional logic to build interactive storytelling elements.
Ever wished you could change the ending of your favorite story or choose your own adventure? 📖 In this lesson, you'll learn how to give users that power in your own Python programs!
We'll explore how to take input directly from a user and then use that input to...
2
Key Concepts & Vocabulary
TermDefinitionExample
User InputInformation or data provided by a user to a computer program, typically through a keyboard.When you type your name into a game, that's user input.
`input()` FunctionA built-in Python function used to get text input from the user. It displays a prompt and waits for the user to type something and press Enter.`name = input('What is your name? ')` will store the user's typed name in the `name` variable.
Conditional StatementsCode structures (`if`, `elif`, `else`) that allow a program to execute different blocks of code based on whether certain conditions are true or false.`if score > 100: print('You win!')`
`if` StatementThe starting point of a conditional statement. It checks if a condition is true; if so, its indented code blo...
3
Core Syntax & Patterns
Getting User Input
variable_name = input("Your prompt message here: ")
Use the `input()` function to display a message to the user and wait for them to type something. Whatever they type (before pressing Enter) will be stored as a string in `variable_name`.
Basic Conditional Logic (if-else)
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
This structure allows your program to make a binary decision. If the `condition` evaluates to `True`, the code under `if` runs. Otherwise, the code under `else` runs. Remember the colon `:` and indentation!
Multi-Choice Conditional Logic (if-elif-else)
if condition1:
# Code for condition1
elif condition2:
# Code for condition2
else:
# Code if no conditions...
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 chooses a class: 'Warrior', 'Mage', or 'Archer'. The program must print a confirmation of their choice, but if they type something else, it should print 'Invalid class'. Which code structure is best for this?
A.An `if` statement for 'Warrior', and a separate `if` statement for 'Mage'.
B.An `if` for 'Warrior', an `elif` for 'Mage', an `elif` for 'Archer', and an `else` for 'Invalid class'.
C.An `if` statement for 'Warrior', and an `else` statement that checks for 'Mage' or 'Archer'.
D.single `if` statement that checks if the input is not 'Invalid class'.
Challenging
A programmer wrote this code to categorize players by age. There is a subtle logical error. Where is it?
`age = int(input("Enter age: "))`
`if age < 13:`
` print("Child rate")`
`elif age < 18:`
` print("Teen rate")`
`else:`
` print("Adult rate")`
A.13-year-old is correctly identified as 'Teen rate', but an 18-year-old is incorrectly identified as 'Teen rate'. The second condition should be `age <= 17` or `age < 18` is fine, but the logic is that the `else` handles 18+. The error is not there. Let's re-read. `age < 13` is for child. If age is 13, this is false. `13 < 18` is true, so it prints 'Teen rate'. Correct. If age is 18, `18 < 13` is false. `18 < 18` is false. It goes to `else` and prints 'Adult rate'. Correct. Let's re-evaluate the options. Maybe I misread the question. Ah, let's re-think the logic. The code is actually correct. The question asks for an error. Let's assume there IS an error and find it. What if the requirement was '13 to 18 inclusive is teen'? Then `age < 18` would be wrong for an 18-year-old. Let's re-write the options to make one clearly correct. Option A: 'An 18-year-old is incorrectly categorized.' No, they are correctly 'Adult'. Option B: 'A 13-year-old is incorrectly categorized.' No, they are correctly 'Teen'. Option C: 'The code works perfectly for all whole numbers.' This seems true. Option D: 'The `else` statement should be `elif age >= 18`'. This is redundant. The logic as written is sound. I must create a question with a real error. Let's modify the code in the question.
Modified Question: A programmer wrote this code to categorize players by age. There is a subtle logical error. Where is it?
`age = int(input("Enter age: "))`
`if age <= 13:`
` print("Child rate")`
`elif age > 13 and age < 18:`
` print("Teen rate")`
`else:`
` print("Adult rate")`
What is the error in this modified code? A 13-year-old is a child. A 14-year-old is a teen. An 18-year-old is an adult. This seems correct. Let's try another modification.
Final Question Version:
A programmer wrote this code to assign movie ticket prices. Child (under 13), Teen (13-17), Adult (18+). Identify the logical error.
`age = int(input("Enter age: "))`
`if age < 13:`
` print("Child ticket")`
`elif age > 13:`
` print("Teen ticket")`
`else:`
` print("Adult ticket")`
B.The code correctly identifies a 12-year-old as 'Child ticket' but incorrectly identifies a 13-year-old as 'Adult ticket'.
C.The code will crash if the user enters an age of 18 or higher.
D.The `int()` function should be `float()` to handle all possible ages.
Challenging
In an interactive story, after a player chooses to 'open the door', you want to give them another choice: 'go left' or 'go right'. How would you structure this in Python?
A.Use a series of `elif` statements for 'open the door', 'go left', and 'go right'.
B.Use one `if` statement that checks for all four possible combinations.
C.Use a separate program for the second choice.
D.Use a nested `if` statement: an inner `if-else` for 'left'/'right' placed inside the `if` block for 'open the door'.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free