User Input & Conditionals in Python
0 views
📘 Week 11 – User Input & Conditionals in Python
Period 1 – User Input & Conditionals
Objective: Learn to get input from the user and use if/elif/else statements to make decisions.
1. The input() Function
- Purpose: Pauses the program, waits for the user to type, and returns the input as a string.
- Syntax:
variable = input("Prompt: ") - Example:
name = input("What is your name? ") print("Hello, " + name + "!")
🔁 Converting Input (Type Casting)
Because input() always returns a string, you must convert it to a number if you need to do math.
age = int(input("How old are you? ")) # → integer
price = float(input("Enter a price: ")) # → float (decimal)
⚠️ Common Pitfall: If the user enters text that can’t be converted (e.g.,
"abc" for an integer), the program will crash with a ValueError. Later we will learn try/except to handle this.
2. Conditional Statements (if, elif, else)
Conditionals let the program choose different paths based on whether a condition is True or False.
Basic structure:
if condition:
# code runs if condition is True
elif another_condition:
# runs if first condition was False and this one is True
else:
# runs if all above conditions are False
🐍 Indentation is critical! Python uses indentation (usually 4 spaces) to indicate which code belongs to each block.
3. Comparison Operators
Used to compare values – they always return True or False.
| Operator | Meaning | Example |
|---|---|---|
== | equal to | age == 18 |
!= | not equal to | age != 0 |
> | greater than | score > 90 |
< | less than | temp < 0 |
>= | greater than or equal | age >= 18 |
<= | less than or equal | price <= 10 |
⚠️ Don’t confuse
Bad:
= (assignment) with == (comparison)!Bad:
if age = 18: ❌ Good: if age == 18: ✅
4. Logical Operators
Combine multiple conditions:
and– both conditions must beTrueor– at least one condition must beTruenot– reverses the truth value
if age >= 18 and has_id:
print("Welcome!")
5. Example – Voting Age Checker (improved)
age = int(input("Enter your age: "))
if age >= 18:
print("You are old enough to vote!")
elif age > 0:
print("Sorry, you are too young to vote.")
else:
print("Invalid age (must be positive).")
6. Bonus – Nested Conditionals
You can put an if inside another if:
age = int(input("Your age: "))
if age >= 18:
country = input("Which country? ")
if country.lower() == "usa":
print("You can vote in the USA!")
else:
print("Check local voting laws.")
else:
print("Too young to vote.")
Period 2 – Exercises: Build Small Python Programs
Objective: Combine input, type conversion, conditionals, and operators to solve practical problems.
📋 Practical Tasks
1️⃣ Greeting Bot
- Ask the user for their name and age.
- Print a personalised greeting that includes both.
- Extra challenge: If the user is 100 or older, congratulate them!
Sample run:
What is your name? Ada
How old are you? 35
Hello, Ada! You are 35 years old.
2️⃣ Simple Calculator
- Ask for two numbers (use
floatto allow decimals). - Ask for an operator (
+,-,*,/). - Use
if/elif/elseto decide the operation. - Print the result.
- Handle division by zero: if the operator is
/and the second number is0, print"Error: Cannot divide by zero."
Sample run:
Enter first number: 10
Enter second number: 3
Enter operator (+, -, *, /): /
10.0 / 3.0 = 3.3333333333333335
3️⃣ Grade Checker
- Ask for a numerical score (0–100).
- Print the corresponding letter grade:
| Score Range | Grade |
|---|---|
| 90 – 100 | A |
| 80 – 89 | B |
| 70 – 79 | C |
| 60 – 69 | D |
| Below 60 | F |
- Bonus: If the score is outside 0–100, print
"Invalid score." - Bonus 2: Add a plus/minus distinction (e.g., 97+ → A+, 93–96 → A, 90–92 → A-).
4️⃣ Temperature Converter (optional challenge)
- Ask the user for a temperature and a scale (
CorF). - If Celsius → Fahrenheit:
F = C * 9/5 + 32 - If Fahrenheit → Celsius:
C = (F - 32) * 5/9 - Print the result with a suitable message.
💬 Wrap‑up discussion points
- Why do we need to convert input with
int()orfloat()? - What happens if we use
=inside anifcondition? - How can we check for multiple conditions at once? (Logical operators)
- Why is indentation so important in Python?
Test yourself on Digital Technology
Introduction to Sequencing, Loops, and Simple Animation in Scratch
134 questions
10 min
SSS1
Start quiz
Track your reading & take quizzes
Create free account