Difference between revisions of "Count the Vowel"
From CompSciWiki
(I organized the page into headers. I also corrected the template to COMP 1010 Extra Labs ; initially, it had 1010PrAD Template.) |
m (changed bullets from <li> to * for consistnecy with other wiki pages) |
||
Line 3: | Line 3: | ||
|Introduction= In this lab, you will learn how to use String methods in Python. | |Introduction= In this lab, you will learn how to use String methods in Python. | ||
==Problem== | ==Problem== | ||
− | Write a Python program that counts the number of vowels in a | + | Write a Python program that counts the number of vowels in a String given by the user. |
|Body= | |Body= | ||
Line 9: | Line 9: | ||
First, we need 3 variables: | First, we need 3 variables: | ||
− | + | * input - holding the user's input | |
− | + | * done - control the while loop | |
− | + | * vowelCount - count the number of vowel | |
{{CodeBlock | {{CodeBlock |
Revision as of 21:30, 4 April 2012
COMP 1010 Home > Back to Extra Labs
IntroductionIn this lab, you will learn how to use String methods in Python. ProblemWrite a Python program that counts the number of vowels in a String given by the user. |
---|
Step 1:
First, we need 3 variables:
- input - holding the user's input
- done - control the while loop
- vowelCount - count the number of vowel
done = False vowelCount = 0
Step 2:
Second, we need get the input using raw_input(prompt).
#get the input from user input = raw_input("Enter a sentence") if input != '':
Step 3:
Third, we need to check the vowel.
input = input.lower() for char in inputs: #//check if it's a vowel if char == 'a' :
Step 4:
Then we need to use while loop to print the output.
while(!done): (...) #output result print '\nVowel count: %d\nVowel percentage: %d%%' % \ (vowelCount, vowelCount * 100 / leng(input))
Now we're done. You should write a complete program.
SolutionCode
def main(): done = False vowelCount = 0 while(!done): input = raw_input("Enter a sentence") if input == '': done = True else: #convert input to lower case input = input.lower() for char in inputs: #//check if it's a vowel if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' : vowelCount++; #increase vowel count print '\nVowel count: %d\nVowel percentage: %d%%' % \ (vowelCount, vowelCount * 100 / leng(input)) main()