问题:你有一个单词,需要计算该单词中辅音的数量。
解决方法: 要计算单词中的辅音,可以遍历单词中的每个字符,检查它是否不是元音。
下面是一个解决这个问题的 Python 代码示例:
def count_consonants (word):
vowels = ['a', 'e', 'i', 'o', 'u']
count = 0
for character in word:
if character not in vowels:
count += 1
return count
# Input word
word = "programming"
# Count the consonants
consonant_count = count_consonants (word)
# Output the result
print (f"The word '{word}' contains {consonant_count} consonants. ")
# The word 'programming' contains 8 consonants.
