Python Program to Identify Vowel or Consonant
user_input = input('Enter a single character from the alphabet: ')
if len(user_input) != 1 or not user_input.isalpha():
print('Error: Please enter a single letter from the alphabet')
else:
letter = user_input.lower()
if letter in ['a', 'e', 'i', 'o', 'u']:
print('Vowel')
else:
print('Consonant')
Explanation:
- Input: The program prompts the user to enter a single character using the
input()function and stores it in theuser_inputvariable. - Validation: It checks if the input length is not equal to 1 (using
len()) or if it's not alphabetic (usingisalpha()). If either condition is true, an error message is printed. - Lowercase Conversion: The user input is converted to lowercase using the
lower()method and stored in thelettervariable. - Vowel/Consonant Check: The program checks if the
letteris present in the list of vowels ('a', 'e', 'i', 'o', 'u'). If it is, it prints 'Vowel'; otherwise, it prints 'Consonant'.
原文地址: https://www.cveoy.top/t/topic/mRSJ 著作权归作者所有。请勿转载和采集!