Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 6th, 2013, 01:48 PM
 tpythonbeginner
Registered User

Join Date: Aug 2013
Posts: 1
Time spent in forums: 35 m
Reputation 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
August 6th, 2013, 02:49 PM
 Good News
Contributing User

Join Date: Jul 2013
Posts: 51
Time spent in forums: 1 Day 23 h 44 m 29 sec
Reputation Power: 1
Quote:
 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])```

 Viewing: Dev Shed Forums > Other > Beginner Programming > Vowel Counter Python 3