编写一个程序来计算一个单词中的音节数。 音节之间由连字符分隔。例如:
- 定义函数
count_syllables()
的函数,该函数接受一个参数word
。 - 在函数内,计算
word
中的音节数并返回它。 - 解析1:
-
def count_syllables(word): # 此处写你的代码 syllables=word.split('-') return len(syllables) # 获取用户输入 word = input() # 调用函数 print(count_syllables(word))
解析2:
-
def count_syllables(word): # 此处写你的代码 syllables=word.count('-') return syllables+1 # 获取用户输入 word = input() # 调用函数 print(count_syllables(word))