-2

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!

pythonguy
  • 11
  • 1
  • 3

4 Answers4

2
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

repzero
  • 8,254
  • 2
  • 18
  • 40
  • This works but do you know how to do it using the .find() built in function? – pythonguy Nov 02 '15 at 23:01
  • 1
    Using the .find() method will return an index number which depicts the position of your pattern in the input string, hence it will not print the number of occurrences of your pattern. .find() == not appropriate – repzero Nov 02 '15 at 23:08
  • but is there not a way to tally up how many indexes have a vowel? Or how many indexes have been found to be a vowel? – pythonguy Nov 02 '15 at 23:11
  • 1
    This answer is exactly how I'd have done it. +1 for you once my vote count refreshes. – BlivetWidget Nov 02 '15 at 23:26
1

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))
twasbrillig
  • 17,084
  • 9
  • 43
  • 67
0

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

  • Yes but like the question says I need to find them using the.find() function and I don't know how to do it... – pythonguy Nov 02 '15 at 22:55
0

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'
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Thanks that's really helpful! I just got a question though, why do I need to do a loop couldn't I just do the .find() and then store the values all at once because won't the function find every 'a' in the word anyways? – pythonguy Nov 02 '15 at 23:18
  • *won't the function find every 'a' in the word anyways?* - no it won't; `help(word.find) > Help on built-in function find: Return the lowest index in S where substring sub is found` - it returns one result, the first place it finds something. – TessellatingHeckler Nov 02 '15 at 23:20
  • ok so then I should be using a while loop inside of an if statement until the .find() can't find any more vowels? I'm really confused :( – pythonguy Nov 02 '15 at 23:21
  • I've added an example to my answer; it prints every time it finds an "a". Could be a bit neater, but I'm trying not to give a complete answer. – TessellatingHeckler Nov 02 '15 at 23:46
  • Thanks a lot I got it to work! Thanks for helping me out I understand what .find() does better! – pythonguy Nov 03 '15 at 00:00