Selection with Casting
You've already learned how to use if-else statements to make decisions based on string comparisons using ==.
Now we're leveling up! Today you'll learn how to:
- Cast user input to integers using
int() - Use comparison operators like
<,>,<=, and>= - Make decisions based on numerical comparisons
Quick Selection Reminder
Remember the basic structure:
if condition:
# Do this if condition is True
else:
# Do this if condition is False
Casting with int()
When you use input(), Python always gives you a string (text), even if the user types a number!
To work with numbers mathematically, we need to cast (convert) them to integers.
# Without casting - this is a STRING
age = input("How old are you? ") # age is "18" (text)
# With casting - this is an INTEGER
age = int(input("How old are you? ")) # age is 18 (number)
Why does this matter? Try this thought experiment:
ans = "18" + "1" # "181" because String concatenation
ans = 18 + 1 # 19 because addition
By wrapping input() with int(), we tell Python:
"Take the user's input and cast it to an integer."
Comparison Operators
You've used == to check if two strings are exactly the same.
But with numbers, we can do so much more. Here are the comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
< |
Less than | 3 < 5 |
True |
> |
Greater than | 7 > 5 |
True |
<= |
Less than or equal to | 5 <= 5 |
True |
>= |
Greater than or equal to | 6 >= 5 |
True |
!= |
Not equal to | 4 != 5 |
True |
These operators let you ask questions like "Is this number bigger than that one?" or "Is this value less than or equal to the limit?"
Combining Casting and Comparison
Now let's put it all together! Here's how we can check if someone is old enough:
AGE_LIMIT = 18
age = int(input("How old are you? "))
if age < AGE_LIMIT:
print("You are too young")
else:
print("You are old enough")
Let's break down what happens:
AGE_LIMIT = 18- We set the limit. Using a constant makes the code easier to understand.age = int(input(...))- We get user input AND cast it to an integerif age < AGE_LIMIT:- We compare the two numbers- The program decides which message to print based on the comparison
Try it mentally: What happens if the user enters 15?
15 < 18 is True, so it prints "You are too young"
What about 21?
21 < 18 is False, so it goes to else and prints "You are old enough"
Complete Example
Here's a full program that checks if someone can watch a 15-rated film:
AGE_LIMIT = 18
age = int(input("How old are you? "))
if age < AGE_LIMIT:
# The condition is True when age is less than AGE_LIMIT
# e.g.: if age is 15, then 15 < 18 is True
print("You are young")
else:
# The condition is False otherwise
# e.g.: if age is 20, then 20 < 18 is False
print("You are old")
Step-by-Step Walkthrough
- Line 1: Create a variable
age_limitand assign the number 18 to it - Line 2: Ask the user "How old are you?", take their answer (e.g., "15"), cast it to an integer (15), and assign it to
age - Line 4: Check if
age < age_limit(is 15 less than 18?) - Line 7: If the condition is True, print "You are young"
- Line 8: If the condition is False, skip to else
- Line 11: Print "You are old"
Remember: Always use int() when you need to do numerical comparisons with user input!
Now it's your turn! Press inspire me to practice with different scenarios involving numbers, limits, and comparisons.
Write a program to...
else: