Adding User Interaction
Overview
Programs are more engaging when they interact with users. You'll integrate input and output elements, handle invalid input safely, and provide clear feedback for a smooth user experience, making your project user-friendly and robust.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- User input: Getting data from users.
- Input validation: Checking that input is correct.
- User feedback: Providing clear messages to users.
- Error messages: Handling invalid input gracefully.
- User experience: Making programs easy to use.
Why User Interaction Matters
Programs that interact well with users are:
More Usable
Users understand what to do and what's happening
More Reliable
Invalid input is handled gracefully
More Professional
Shows attention to detail and user needs
More Enjoyable
Users have a better experience
Getting User Input
Use input() to get data from users:
# Simple input
name = input("Enter your name: ")
print(f"Hello, {name}!")
# Input with validation
while True:
age = input("Enter your age: ")
if age.isdigit():
age = int(age)
break
else:
print("Please enter a valid number.")
# Menu-driven interface
def show_menu():
print("1. Add task")
print("2. View tasks")
print("3. Exit")
choice = input("Choose an option: ")
return choice
Input Validation
Always validate user input to prevent errors:
Get Input
Use input() to get user data
Check Validity
Verify input meets requirements
Handle Invalid Input
Show error message and ask again
Use Valid Input
Proceed with validated data
def get_task_number(max_tasks):
"""Get valid task number from user."""
while True:
try:
number = int(input(f"Enter task number (1-{max_tasks}): "))
if 1 <= number <= max_tasks:
return number
else:
print(f"Please enter a number between 1 and {max_tasks}.")
except ValueError:
print("Please enter a valid number.")
Providing Feedback
Give users clear feedback about what's happening:
Feedback Best Practices
- Confirm actions: "Task added successfully!"
- Show results: Display what happened
- Error messages: Explain what went wrong
- Instructions: Tell users what to do next
# Confirmation messages
print("✓ Task added successfully!")
print("✓ Task marked as complete!")
# Error messages
print("✗ Invalid input. Please try again.")
print("✗ Task not found. Please check the number.")
# Status updates
print(f"Loading {len(tasks)} tasks...")
print("Saving your data...")
Summary
In this lesson, you learned:
- User input: Get data using
input() - Validation: Always check input is valid
- Feedback: Provide clear messages to users
- Error handling: Handle invalid input gracefully
- User experience: Make programs easy to use
Remember
Good user interaction makes your project professional and user-friendly. Always validate input and provide clear feedback. Your users will thank you!
End-of-Lesson Exercises
Think about these questions to reinforce what you've learned:
Exercise 1: User Interaction
Why is user interaction important? How do you get and validate user input?
Exercise 2: Feedback
What makes good user feedback? Give examples of effective feedback messages.