Practical 5: Program to Determine Whether the Character Entered is a Vowel or Not
Problem Statement
Write a Python program to determine whether the character entered by the user is a vowel or not. The program should:
- Accept a character input from the user.
- Check if the character is one of the vowels (a, e, i, o, u), regardless of whether it’s uppercase or lowercase.
- Print a message indicating whether the entered character is a vowel or not.
Solution Code
# Program to check if the entered character is a vowel or not
# Input: Take a character from the user
char = input("Enter a character: ").lower() # Convert to lowercase to handle case insensitivity
# Check if the character is a vowel
if char in 'aeiou':
print(f"{char} is a vowel.")
else:
print(f"{char} is not a vowel.")
Expected Outcome
- The program will prompt the user to input a character.
- It will then print whether the character is a vowel or not based on the comparison.
Example outputs for different inputs:
For vowel letter:
Enter a character: a
a is a vowel.
Process finished with exit code 0
For vowel letter:
Enter a character: g
g is not a vowel.
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 2 Practical 5 solution where we have written Python program to determine whether the character entered by the user is a vowel or not
- Input of Character:
- The program asks the user to input a character. It then converts the character to lowercase using the lower() method. This ensures that the program works case-insensitively, meaning both uppercase and lowercase vowels will be accepted.
- Vowel Check:
- The program uses the in operator to check if the entered character is one of the vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’).
- If the character is found in the string ‘aeiou’, it prints that the character is a vowel. Otherwise, it prints that the character is not a vowel.
- Displaying the Result:
- The program displays whether the character is a vowel or not based on the condition check.
Key Concept Learned
- Character Comparison:
- This practical demonstrates how to compare a single character with a set of values (in this case, the vowels) using the in operator.
- Case Insensitivity:
- By converting the input to lowercase, the program ensures that it can handle both uppercase and lowercase vowels, making it more flexible.
- String Handling:
- The program shows how to work with strings (checking if a character exists in a string) and how to use string methods like lower() to manipulate case.
- Conditional Statements:
- The program uses an if-else statement to check if the entered character is a vowel and displays the appropriate message.