编写一个程序来计算一个单词中的音节数。
音节之间由连字符分隔。例如:
beau-ti-ful => 三个音节
cat => 一个音节
re-frig-er-a-tor => 五个音节
定义函数count_syllables()的函数,该函数接受一个参数word。
在函数内,计算word中的音节数并返回它。
- 这道题关键在于如何通过‘-’计算,str.count(字符串,start,end)
点击查看代码
def count_syllables(word):return word.count('-',0,len(word))+1# 获取用户输入
word = input()
# 调用函数
print(count_syllables(word))