Drop #4 – Interacting with Users

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:

  1. It prints a prompt (optional) to the console.
  2. It waits for the user to type something and hit Enter.
  3. It returns the input as a string.

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:

  • Web applications: You’ll use web forms instead.
  • GUIs: Frameworks like Tkinter, PyQt, or Kivy provide more interactive widgets.
  • Automated scripts: Here, parameters are often passed via command-line arguments (sys.argv or argparse).

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:

  1. Asks for the user's name
  2. Asks for their favorite programming language
  3. Asks how many years they've been coding
  4. Then prints a summary sentence like:

"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



Eyji K.

Software Engineer | Python, Django, AWS, RAG

1mo

Thanks for sharing, Arthur

Like
Reply
Fernando Miyahira

Software Engineer | Mobile Developer | Flutter | Dart

1mo

Thanks for sharing, Arthur

Leonardo Andrade

FullStack Software Engineer | React • React Native • TypeScript • Node.js • AWS

1mo

Thanks for sharing, Arthur

🚀 Maxim Varban

Senior Software QA Engineer | 6+ years | Java | Selenium | Docker | Kubernetes

1mo

Loving 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?

Paulo Rocha

QA | Quality Assurance Engineer | SDET | Cypress | Selenium | RestAssured | Appium | CTFL | API Testing | Automation Framework

1mo

Great insights! User interaction isn't just about features — it's about creating meaningful, engaging experiences that truly connect.

To view or add a comment, sign in

Others also viewed

Explore topics