Write a program that prompts the user to provide a single character from the alphabet Print Vowel or Consonant depending on the user input If the user input is not a letter between a and z or A and Z
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:
- Prompt the user for input using the
input()function and store it in a variable calleduser_input. - Check if the length of the user input is not equal to 1 using the
len()function and if the input is not alphabetic using theisalpha()function. If either of these conditions is true, print an error message. - Convert the user input to lowercase using the
lower()method and store it in a variable calledletter. - Check if the
letteris one of the vowels ('a', 'e', 'i', 'o', 'u'). If it is, print "Vowel". Otherwise, print "Consonant".
原文地址: https://www.cveoy.top/t/topic/bo0x 著作权归作者所有。请勿转载和采集!