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:

  1. Input: The program prompts the user to enter a single character using the input() function and stores it in the user_input variable.
  2. Validation: It checks if the input length is not equal to 1 (using len()) or if it's not alphabetic (using isalpha()). If either condition is true, an error message is printed.
  3. Lowercase Conversion: The user input is converted to lowercase using the lower() method and stored in the letter variable.
  4. Vowel/Consonant Check: The program checks if the letter is present in the list of vowels ('a', 'e', 'i', 'o', 'u'). If it is, it prints 'Vowel'; otherwise, it prints 'Consonant'.
Python Program to Identify Vowel or Consonant

原文地址: https://www.cveoy.top/t/topic/mRSJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录