User Input

📘 Lesson 3: Getting Input from the User

What is input()?

So far, our Python programs have displayed information that we wrote ourselves.

But what if we want the computer to ask the user a question and use their answer?

Python uses the input() function to collect information from the user.

This makes our programs interactive because the user can type their own answers.


Why Do We Use input()?

The input() function allows a program to:

  • 👤 Ask for a person’s name
  • 🎂 Ask for their age
  • 🏫 Ask for their school name
  • 📚 Ask for their favorite subject
  • 🎮 Ask for their favorite game
  • 🤖 Create interactive AI-style conversations

Without input(), every program would always show the same information.


Real-Life Example

Imagine a teacher asking every student:

“What is your name?”

Each student gives a different answer.

Python can do exactly the same thing!


Your First input() Program

name = input("What is your name? ")

print("Hello,", name)

Example

If the user types:

Ali

The program displays:

Hello, Ali

Asking More Than One Question

Python can ask several questions.

name = input("What is your name? ")
age = input("How old are you? ")

print("Hello,", name)
print("You are", age, "years old.")

Example Output

What is your name?
Ali

How old are you?
11

Hello, Ali
You are 11 years old.

Asking About School

school = input("What is your school name? ")

print("School:", school)

Example Output

School: City School

Creating a Friendly Greeting

name = input("Enter your name: ")
subject = input("Favorite subject: ")

print("Hello,", name)
print("Your favorite subject is", subject)

How AI Uses input()

AI chatbots like ChatGPT first receive your question as input.

Then they process your input and generate a helpful response.

Every conversation with an AI begins with user input.


🎯 Mission 3

Open the 💻 Python Studio and write this program.

name = input("Enter your name: ")

print("Welcome to LearnWithQazi,", name)

Challenge 1

Replace the greeting with your own message.

Example:

Welcome, Ali!

Challenge 2

Create this program.

name = input("Name: ")
school = input("School: ")

print(name)
print(school)

Challenge 3 ⭐

Ask three questions.

  • Name
  • Age
  • Favorite Subject

Then display all three answers.


🎮 Activity

Imagine you are creating your own AI assistant.

Write three questions you would like your AI assistant to ask.


💡 Did You Know?

Whenever you log in to a website and type your username or password, you are providing input.

Games, websites, mobile apps, and AI systems all use user input to interact with people.



⭐ Lesson Summary

Today you learned:

  • ✅ What the input() function is.
  • ✅ How to ask users questions.
  • ✅ How to save user input in variables.
  • ✅ How to display personalized messages.
  • ✅ How interactive programs and AI systems use user input.

🎉 Excellent work! Your Python programs are now interactive.