更新时间:2023-02-26 13:39:52
问题是您给它提供了大写的字符串,但只与小写的值进行了比较.可以通过在函数的开头添加 word = word.lower()
来解决此问题.
The problem is that you're giving it an uppercase string, but you only compare to lowercase values. This can be fixed by adding word = word.lower()
to the start of your function.
def syllable_count(word):
word = word.lower()
count = 0
vowels = "aeiouy"
if word[0] in vowels:
count += 1
for index in range(1, len(word)):
if word[index] in vowels and word[index - 1] not in vowels:
count += 1
if word.endswith("e"):
count -= 1
if count == 0:
count += 1
return count
print(syllable_count('HAIRY')) # prints "2"