title: Count the Number of Syllables in a Word
body: <p>I am a beginner, and I have a question that I need help with. It is homework so any hints are appreciated. I have seen a few similar topics, but the answers are beyond what I know how to do yet...</p>
  
  <p>I need to count the number of syllables in a text file as part of a larger program. I've got everything I need except for syllables. I have tried a couple of different methods but it does not always catch special cases. I am supposed to 'count groups of adjacent vowels, not including 'e' at the ends of words.' I understand what that means, but I am not able to get it right in my program. Here is what I have:::</p>
  
  <pre><code>def syllables(word):
      syl = 0
      vowels = 'aeiouy'
      starts = ['ou','ei','ae','ea','eu','oi']
      endings = ['es','ed','e']
      word = word.lower().strip(".:;?!")
      for vowel in vowels:
          syl +=word.count(vowel)
      for ending in endings:
          if word.endswith(ending):
              syl -=1
      for start in starts:
          if word.startswith(start):
              syl -=1
      if word.endswith('le'):
          syl +=1
      if syl == 0:
          syl+=1
      return syl
  </code></pre>
  
  <p>EDIT: New code </p>
  
  <pre><code>def syllables(word):
      count = 0
      vowels = 'aeiouy'
      word = word.lower().strip(".:;?!")
      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 word.endswith('le'):
          count+=1
      if count == 0:
          count +=1
      return count
  </code></pre>