3

I am trying to write a code with python that will find and count the number of vowels in any given string. My function prints out 0 vowels no matter what. Any tips?

txt = input("Type text: ").lower()

def findVowels():
    global txt
    vowels = 0
    for letter in txt:
        if letter == 'aeiouy':
            vowels += 1
    return vowels


print(findVowels())

Number of vowels = 0 no matter what

James
  • 32,991
  • 4
  • 47
  • 70
mikkelven
  • 65
  • 5
  • This is may not be code related but technically, "y" is not a vowel. – CodeRed Oct 02 '19 at 09:00
  • @CodeRed In English, "y" is considered as a vowel when it is not the first letter of syllable within a word. I.e. it is a consonant in the word "yup", but a vowel in the word "symbol". – James Oct 02 '19 at 09:02
  • I'd love to explain but this is not the best platform for that. Good day sir! – CodeRed Oct 02 '19 at 09:06
  • In my language, Norwegian "y" is a vowel so that's why it's in there – mikkelven Oct 11 '19 at 13:29

2 Answers2

4

Try like this:

def findVowels(txt):
    return sum(1 for t in txt if t in 'aeiouy')
zipa
  • 27,316
  • 6
  • 40
  • 58
2

You are checking if the letter is equal to the entire string 'aeiouy'. Instead check if letter is in the string.

if letter in 'aeiouy':

Also, you can avoid using global variable by passing the input text to the function.

txt = input("Type text: ").lower()

def findVowels(txt):
    vowels = 0
    for letter in txt:
        if letter in 'aeiouy':
            vowels += 1
    return vowels


print(findVowels(txt))
ruohola
  • 21,987
  • 6
  • 62
  • 97
James
  • 32,991
  • 4
  • 47
  • 70