#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Vowel Counter Python 3


    hello! First post

    I must create two functions, one that asks user for a character and determines whether a character is a vowel or isn't by returning true or false.

    The second is to call the first function and ask the user to input a word.

    This is what I have so far:

    vowel = ["A","E","I","O","U","a","e","i","o","u"]
    vowelinpt = input("Please enter a character: ")
    def isVowel(x):
    if x in vowel:
    return True
    else: return False
    a = isVowel(vowelinpt)
    print(a)

    def countvowel(b)
    count 0
    for a in b:
    if isVowel(a) == True
    count +=1
    return count
    x = input("Please input a WORD: ")
    y = countvowel(x)
    print(y)

    Any ideas on how to actually make it count the correct number of vowels?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    Originally Posted by tpythonbeginner
    hello! First post

    I must create two functions, one that asks user for a character and determines whether a character is a vowel or isn't by returning true or false.

    The second is to call the first function and ask the user to input a word.

    This is what I have so far:

    vowel = ["A","E","I","O","U","a","e","i","o","u"]
    vowelinpt = input("Please enter a character: ")
    def isVowel(x):
    if x in vowel:
    return True
    else: return False
    a = isVowel(vowelinpt)
    print(a)

    def countvowel(b)
    count 0
    for a in b:
    if isVowel(a) == True
    count +=1
    return count
    x = input("Please input a WORD: ")
    y = countvowel(x)
    print(y)

    Any ideas on how to actually make it count the correct number of vowels?
    You should read a bit about scopes. Functions have a local scope that is separate from global. The vowel list can't be access from inside the function unless it is created their or you define 'global vowel'. this code below will count how many vowels:
    Code:
    def vowelcount(word):
    	vowels = ['a', 'e', 'i' ,'o', 'u']
    	count = 0
    	for letter in word:
    		if letter.lower() in vowels:
    			count += 1
    	return count
    or a shorter version:
    Code:
    def vowelcount(word):
    	vowels = ['a', 'e', 'i', 'o', 'u']
    	return len([letter for letter in word if letter.lower() in vowels])

IMN logo majestic logo threadwatch logo seochat tools logo