To handle different inputs in Python, you can use conditional statements to check the type or value of the input. Here’s an example that demonstrates how to handle different types of inputs:
# Asking for user input
user_input = input("Please enter a number, 'yes', or 'no': ")
# Handling different inputs
if user_input.isdigit():
number = int(user_input)
print(f"You entered a number: {number}")
elif user_input.lower() == 'yes':
print("You entered 'yes'.")
elif user_input.lower() == 'no':
print("You entered 'no'.")
else:
print("Invalid input. Please enter a number, 'yes', or 'no'.")
In this example:
- The program checks if the input is a digit using
isdigit(). - It also checks for the strings 'yes' and 'no', ignoring case with
lower(). - If the input doesn't match any of the expected values, it prints an error message.
