Welcome Back to Day 4! 👋
Hey everyone! It’s Day 4 of my 30 Days of Python Challenge, and today is super exciting because we’re making our programs interactive!
If you missed the previous days:
- [Day 1: Print Statements]
- [Day 2: Variables and Data Types]
- [Day 3: Type Casting]
Today, we’re learning how to ask users questions and get their responses. Let’s make our programs come alive!
💬 Day 4: User Input - Having a Conversation with Your Code
Today’s mission: User Input. Until now, we’ve been writing all the data ourselves. But what if we want our program to ask questions and respond based on what the user says? That’s what the input() function is for!
What I Learned
The input() function is like having a conversation with your program. Yo…
Welcome Back to Day 4! 👋
Hey everyone! It’s Day 4 of my 30 Days of Python Challenge, and today is super exciting because we’re making our programs interactive!
If you missed the previous days:
- [Day 1: Print Statements]
- [Day 2: Variables and Data Types]
- [Day 3: Type Casting]
Today, we’re learning how to ask users questions and get their responses. Let’s make our programs come alive!
💬 Day 4: User Input - Having a Conversation with Your Code
Today’s mission: User Input. Until now, we’ve been writing all the data ourselves. But what if we want our program to ask questions and respond based on what the user says? That’s what the input() function is for!
What I Learned
The input() function is like having a conversation with your program. You can:
- Ask users questions
- Store their responses in variables
- Use those responses to create personalized experiences
- Make your code dynamic and interactive!
Plus, I discovered f-strings - a super clean way to combine text and variables!
My Code
Here’s what I wrote for Day 4:
# Day 4 - User input
name = input("What's your name? ")
age = input("How old are you? ")
print(f"Hello {name}, you are {age} years old.")
height = input("What's your height in meters? ")
is_student = input("Are you a student? (yes/no) ").lower() == 'yes'
print(f"Your height is {height} meters and it is {'true' if is_student else 'false'} that you are a student.")
Breaking It Down 🔍
Let me walk you through each part:
name = input("What's your name? ") - The input() function displays the question and waits for the user to type something. Whatever they type gets stored in the name variable.
1.
age = input("How old are you? ") - Same thing here! The program pauses, waits for input, and stores it in age.
1.
print(f"Hello {name}, you are {age} years old.") - This is an f-string! Notice the f before the quotes? It lets me put variables directly inside the text using curly braces {}. So much cleaner than using commas!
1.
input("What's your height in meters? ") - Getting more user input!
1.
.lower() == 'yes' - This is clever! The .lower() converts the input to lowercase (so “YES”, “Yes”, or “yes” all work), then checks if it equals ‘yes’. This creates a boolean (True/False) value.
1.
{'true' if is_student else 'false'} - This is a conditional expression inside the f-string. It displays ‘true’ if is_student is True, otherwise ‘false’. Pretty neat!
Output
When you run this code and interact with it, you’ll see something like:
What's your name? Sanaipei
How old are you? 23
Hello Sanaipei, you are 23 years old.
What's your height in meters? 5.213
Are you a student? (yes/no) no
Your height is 5.213 meters and it is false that you are a student.
🎯 Cool Things I Discovered
Today I learned several new concepts:
input()- Pauses the program and waits for user input- F-strings (
f"text {variable}") - Modern way to format strings in Python .lower()- Converts text to lowercase- Conditional expressions - Quick if/else inside expressions
- All
input()returns strings (remember Day 3’s type casting? Super useful here!)
🤔 Why F-Strings Are Awesome
Compare these two approaches:
Old way:
print("Hello", name, ", you are", age, "years old.")
F-string way:
print(f"Hello {name}, you are {age} years old.")
F-strings are cleaner, more readable, and you have better control over spacing and formatting!
💡 Key Takeaways
input()makes your programs interactive by accepting user responses- All input comes in as strings (use type casting if you need numbers!)
- F-strings (formatted string literals) are the modern way to combine text and variables
- Use
.lower()to make input case-insensitive - You can use conditional expressions inside f-strings for dynamic messages
- Always include a prompt message in
input()so users know what to enter
🚀 What’s Next?
Tomorrow on Day 5, I’ll be exploring arithmetic operators - learning how to do calculations in Python. We’ll add, subtract, multiply, divide, and more! Time to make Python our calculator! 🧮
💬 Let’s Connect!
I’d love to hear from you!
- What’s the first interactive program you want to build?
- Have you tried f-strings yet? What do you think?
- Any creative ideas for using user input?
Drop a comment below! If you’re coding along, share what questions you’re asking users in your programs! 🎤
Don’t forget to follow me for daily updates. Day 5 is going to be mathematical! 💪
*Happy Coding! *