Drop #4 – Interacting with Users
If your program isn’t talking to anyone, it’s not useful yet.
Interactivity is at the heart of most software. Whether you're writing a small script, a system tool, or the foundation for a more complex application, sooner or later you’ll need to capture user input at runtime.
In Python, it all starts with the humble, yet powerful: input() function.
📥 Understanding input()
The input() function does two things:
Example:
name = input("Enter your name: ")
print(f"Welcome, {name}!")
Even if the user types 42, the value of name will still be '42' — a string.
✅ Key Insight: input() is blocking: your program stops until the user provides input. This is both a feature (for CLI interactions) and a limitation (for non-blocking systems like GUIs or servers).
🧩 Building Better Prompts
A common mistake: writing vague or unfriendly prompts.
❌ Bad:
x = input("x: ")
✅ Better:
x = input("Enter the number of items you want to purchase: ")
The clearer your prompt, the better the experience — especially when building command-line interfaces (CLIs) for others to use.
🚨 The Type Problem
All data from input() is a string. That’s a problem when you expect numbers, dates, or structured data.
age = input("Enter your age: ")
print(age + 1) # 🔴 TypeError: can only concatenate str (not "int") to str
To fix this, we need explicit type conversion:
age = int(input("Enter your age: "))
print(f"Next year you'll be {age + 1}.")
But what if the user types "twenty" instead of 20?You’ll get a ValueError. That's why input validation is key, but we’ll explore this in depth in a later drop.
But what if the user types "twenty" instead of 20? You’ll get a ValueError. That's why input validation is key, but we’ll explore this in depth in a later drop.
⚡ Bonus: Dynamic CLI Programs
Here’s a more practical example, a mini CLI questionnaire:
def ask_user():
name = input("What's your name? ")
try:
age = int(input("How old are you? "))
except ValueError:
print("That's not a valid number for age.")
return
print(f"\nHello, {name}!")
print(f"In 5 years, you'll be {age + 5}.")
ask_user()
✅ Pro Tip: Always wrap conversions in try/except blocks when dealing with real users — they will always surprise you.
🔍 When not to use input()?
While input() is great for simple programs and prototypes, it’s not recommended for:
Still, mastering input() teaches the fundamentals of interactivity, parsing, and validation — skills that scale to more advanced interfaces.
👨💻 Mini Challenge: Design a CLI script that:
"Alice, you’ve been coding in Python for 5 years. Impressive!"
Drop your solution in the comments, let’s share and learn from each other.
This is part of the Python Drops series, practical, professional, and progressive insights into Python.
👉 Next up: Converting Data Types in Python, because not everything in life is a string.
#SoftwareEngineer #PythonDrops
Software Engineer | Python, Django, AWS, RAG
1moThanks for sharing, Arthur
Software Engineer | Mobile Developer | Flutter | Dart
1moThanks for sharing, Arthur
FullStack Software Engineer | React • React Native • TypeScript • Node.js • AWS
1moThanks for sharing, Arthur
Senior Software QA Engineer | 6+ years | Java | Selenium | Docker | Kubernetes
1moLoving the vibe and clean design on this one 👌 Also, great reminder of how Python’s flexibility keeps inspiring creativity in both code and visuals. Did you build this for a specific project or just for fun?
QA | Quality Assurance Engineer | SDET | Cypress | Selenium | RestAssured | Appium | CTFL | API Testing | Automation Framework
1moGreat insights! User interaction isn't just about features — it's about creating meaningful, engaging experiences that truly connect.