E EpicCBT
Home Lesson Notes Quiz Center Leaderboard Login
All notes Digital Technology · User Input & Conditionals in Python · SSS1

User Input & Conditionals in Python

0 views
User Input & Conditionals in Python
Week 11 – User Input & Conditionals

📘 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.

OperatorMeaningExample
==equal toage == 18
!=not equal toage != 0
>greater thanscore > 90
<less thantemp < 0
>=greater than or equalage >= 18
<=less than or equalprice <= 10
⚠️ Don’t confuse = (assignment) with == (comparison)!
Bad: if age = 18: ❌    Good: if age == 18:

4. Logical Operators

Combine multiple conditions:

  • and – both conditions must be True
  • or – at least one condition must be True
  • not – 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 float to allow decimals).
  • Ask for an operator (+, -, *, /).
  • Use if/elif/else to decide the operation.
  • Print the result.
  • Handle division by zero: if the operator is / and the second number is 0, 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 RangeGrade
90 – 100A
80 – 89B
70 – 79C
60 – 69D
Below 60F
  • 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 (C or F).
  • 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() or float()?
  • What happens if we use = inside an if condition?
  • How can we check for multiple conditions at once? (Logical operators)
  • Why is indentation so important in Python?

Test yourself on Digital Technology

User Input & Conditionals in Python

132 questions 10 min SSS1
Start quiz

Introduction to Python

148 questions 10 min SSS1
Start quiz

Interactivity & Debugging in Scratch

142 questions 10 min SSS1
Start quiz

Introduction to Sequencing, Loops, and Simple Animation in Scratch

134 questions 10 min SSS1
Start quiz

Algorithms & Block-Based Programming

148 questions 10 min SSS1
Start quiz

Internet Architecture & Information Literacy

131 questions 10 min SSS1
Start quiz

Track your reading & take quizzes

Create free account