编写一个 Python 程序,通过一个实际示例来检查字符是元音还是辅音。
此 Python 程序允许用户输入任何字符。接下来,我们使用 If Else 语句来检查用户输入的字符是元音还是辅音。
在这里,If 语句检查 Python 字符是否等于 a、e、i、o、u、A、E、I、O、U。如果为 TRUE,则为元音。否则,为辅音。
ch = input("Please Enter Your Own Character : ")
if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u' or ch == 'A'
or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'):
print("The Given Character ", ch, "is a Vowel")
else:
print("The Given Character ", ch, "is a Consonant")

Python 程序:使用 ASCII 值检查字符是元音还是辅音
在此示例中,我们使用 ASCII 值来检查给定的字符是元音还是辅音。
ch = input("Please Enter Your Own Character : ")
if(ord(ch) == 65 or ord(ch) == 69 or ord(ch) == 73
or ord(ch) == 79 or ord(ch) == 85
or ord(ch) == 97 or ord(ch) == 101 or ord(ch) == 105
or ord(ch) == 111 or ord(ch) == 117):
print("The Given Character ", ch, "is a Vowel")
elif((ord(ch) >= 97 and ord(ch) <= 122) or (ord(ch) >= 65 and ord(ch) <= 90)):
print("The Given Character ", ch, "is a Consonant")
Please Enter Your Own Character : E
The Given Character E is a Vowel
>>>
Please Enter Your Own Character : l
The Given Character l is a Consonant