要求:
编写一个程序来计算给定单词中辅音字母和元音字母的数量。
- 定义函数
count_consonants()
,参数为word
。该函数应返回单词中辅音字母的数量。 - 定义函数
count_vowels()
,参数为word
。该函数应返回单词中元音字母的数量。
示例输入
Programming
示例输出
8
3
- 忽略数字,特殊字符和空格。
- 元音字母为:
aeiou
代码:
ss = "aeiou"
s = "bcdfghjklmnpqrstvwxyz"
def count_consonants(word):
count1 = 0
str1 = word.lower()
for s1 in str1:
if s1 in s:
count1+=1
return count1
def count_vowels(word):
count2 = 0
str2 = word.lower()
for s2 in str2:
if s2 in ss:
count2+=1
return count2
# 获取用户输入
word = input()
# 调用函数
print(count_consonants(word))
print(count_vowels(word))
注意:
for 和 if 后面参数填写不正确,会导致结果不对
主要还是逻辑问题会导致出现上面的错误