写一个 Python示例来检查字符是元音还是辅音,并给出一个实例。
这个 python 程序允许用户输入任何字符。接下来,我们使用 If Else 语句来检查用户给定的字符是元音还是辅音。
这里, If 语句检查字符是否等于 A、E、I、O、u、A、E、I、O、u,如果为 TRUE,则为元音。否则就是辅音。
# Python Program to check character is Vowel or Consonant
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 值来检查给定的字符是元音还是辅音。
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
标签: 算法