How to seperate vowels and constants when a user inputs a word Python

Question:

Write a program that lets you enter a word and that prints out the number of vowels and the number of consonants (vowels are: a,e,i,o,u. all others are consonants). The program should repeat asking for more words, until you enter “stop”
HINT: use build in find() function.

Here is what I have so far:

word = raw_input('Enter a word')
print word.find("a"), word.find("e"), word.find('i'), word.find('o'), word.find('u')

I am really lost as to what to do next can someone show me how to use the find function properly because it doesn’t seem to be working the way I expected it to work, but it is not. In this code I need to use the .find() built in function without the use of if statements and finding if the values are ‘a’ or ‘e’ and so forth!

Asked By: pythonguy

||

Answers:

You should try to use loops so that the user can write multiple entries, and to count the number of vowels. Or use the function count if possible

getinput=""
while getinput != "stop":
    getinput=raw_input("Enter a word: ")
    vowels=len([v for v in getinput if v in "aeiou"])
    consonants=len([v for v in getinput if v not in "aeiou"])
    print("No. of vowels in this word:",vowels)
    print("No. of consonants in this word:",consonants)

python2.7 script

Answered By: repzero

Well done for writing a question and having some code to post.

You didn’t say what you expect find() to do, but I guess you expect it to return how many times it found something? Nope; count() does that; you could (word.count('a') + word.count('e')...) to solve this, but your hint is to use find(), so that’s out.

find() returns where it found something, or -1 if it found nothing.

You’re going to have to word.find('a') and then store the result, check if it’s a location in the string or a -1 to say it found nothing. Then word.find('a', location+1) to search from just after the find location, and search the remaining characters. Check the return value of that to see if it found anything, then keep doing that in a loop until it finds nothing. Keep track of how many times it looped.

Then do that for ‘e’, ‘i’, ‘o’, ‘u’. (a loop inside a loop).

Add them all up, that’s the number of vowels. Take len(word) - num_vowels and that’s the number of consonants…

unfinished example:

word = 'alfalfa'

location = word.find('a')
if location > -1: 
    print 'found "a", count this'

while location > -1:
    location = word.find('a', location + 1)
    if location > -1:
        print 'found another "a", count this'
Answered By: TessellatingHeckler

Use a regex. It simplifies things in this case.

import re
word = raw_input('Enter a word')
numvowels = len(re.findall("[aeiou]", word))
numconsonants = len(word) - numvowels
print("Number of vowels is {} and number of consonants is {}".format(numvowels, numconsonants))
Answered By: twasbrillig
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.