How to handle different inputs?

QuestionsQuestions8 SkillsProYour First Python LabAug, 23 2025
0155

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.

0 Comments

no data
Be the first to share your comment!