Skip to content

User Input and Display ⌨️

Mentor's Note: A program that doesn't take input is like a radioβ€”it only talks. A program that takes input is like a conversation! πŸ’‘


🌟 The Scenario: The Magic Mirror πŸͺž

Imagine you have a magic mirror that repeats whatever you say.

  • The Logic: You give it a word (Input). πŸ“¦
  • The Result: It shows you the word back (Output). βœ…

πŸ’» Implementation: The Greeting Lab

# πŸ›’ Scenario: Greeting a User
# πŸš€ Action: Taking input and displaying it

# 1. Taking String Input (Text) πŸ“›
name = input("Enter your name: ")  # Prompts user and reads input as a string
print("Hello,", name + "!")

# 2. Taking Integer Input (Numbers) πŸ”’
# input() always returns a string, so we MUST convert it to an integer
number_str = input("Enter your favorite number: ")
number = int(number_str) # Convert the string to an integer
print(f"Your favorite number is: {number}")

πŸ“– Key Concepts

  • input(): This function displays a message (the prompt) and waits for the user to type something and press Enter.
  • Always a String: Everything you type into input() is treated as text. If you want a number, you must convert it using int() or float().
  • f-strings: Using f"Text {variable}" is the modern, professional way to combine text and data in Python.

🧠 Step-by-Step Logic

  1. Start 🏁
  2. Prompt the user: "Enter your name: ".
  3. Wait for the user to type and press Enter.
  4. Store that value in the variable name.
  5. Display "Hello, " followed by the name.
  6. End 🏁

🎯 Practice Lab πŸ§ͺ

Task: Age Calculator

Task: Ask the user for their birth year, calculate their age (2026 - birth_year), and print it. Hint: Don't forget to use int() for the year! πŸ’‘

Quick Quiz

Quick Quiz

If you type 10 into an input() prompt, what is the data type of the result? - [ ] int - [x] str - [ ] float - [ ] boolean

Explanation: The input() function always returns a string (str), even if you type a number. You must use int(input()) to get an integer.


πŸ“ˆ Learning Path