Write a program in python that reads a word and prints the number of syllables i
ID: 3671830 • Letter: W
Question
Write a program in python that reads a word and prints the number of syllables in the word. For this program, assume the following rules are the only ones you need to determine the number of syllables (there are some special case words that do not follow these rules and we will NOT test for those):
Each vowel (a,e,i,o,u,y) counts as 1 syllable except as noted below
A sequence of adjacent vowels count as 1 syllable
An ‘e’ at the end of a word does not count
If the program determines there are no syllables, then set the count to 1 syllable
Here are some examples:
Please give me a word: harry
The word harry has 2 syllables.
Please give me a word: hairy
The word hairy has 2 syllables.
Please give me a word: hare
The word hare has 1 syllables.
Please give me a word: the
The word the has 1 syllables.
Explanation / Answer
#if raw_input is not working then use input(), its because of the different versions of python
def count_syllable(word):
count = 0
vowels = ['a','e','i','o','u','y']
i = 0
length = len(word)
while i<length-1: #I am not checking last character in word
if word[i] in vowels: #Each vowel (a,e,i,o,u,y) counts as 1 syllable
i = i+1
#A sequence of adjacent vowels count as 1 syllable
#Checking for sequence
while i<length-1:
if word[i] not in vowels:
break
else:
i = i+1
count = count+1 #Each vowel (a,e,i,o,u,y) counts as 1 syllable
i = i+1
#Now checking last character in word
if word[-1] in ['a','i','o','u','y']: #An 'e' at the end of a word does not count
if length>=2:
if word[-2] not in vowels: #checking if its sequence or not
count = count+1
if count == 0: #If the program determines there are no syllables, then set the count to 1 syllable
count = 1
print "Number of syllables = ",count," "
word = raw_input("Please give me a word: ");
count_syllable(word)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.