Difference between revisions of "Count the Vowel"

From CompSciWiki
Jump to: navigation, search
m (Change we've to we're. Added in more missing periods to sentences.)
m
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{1010PrAD
+
{{Template:1010ExtraLabs
|ProblemName=Vowel Counter
+
|Chapter_TOC=[[Extra Labs]]
|Problem=Count the number of vowels in a string given by the user.
+
|Introduction= In this lab, you will learn how to use String methods in Python.
|SideSectionTitle=String Methods
+
==Problem==
|Solution=
+
Write a Python program that counts the number of vowels in a String given by the user.
 
+
|Body=
  
 +
===Step 1:===
 
First, we need 3 variables:
 
First, we need 3 variables:
<li>input - holding the user's input
+
 
<li>done - control the while loop
+
* input - holding the user's input
<li>vowelCount - count the number of vowel  
+
* done - control the while loop
 +
* vowelCount - count the number of vowel  
 +
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 16: Line 19:
 
}}<br />
 
}}<br />
  
 +
===Step 2:===
 
Second, we need get the input using raw_input(prompt).
 
Second, we need get the input using raw_input(prompt).
 +
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 25: Line 30:
 
}}<br />
 
}}<br />
  
 +
===Step 3:===
 
Third, we need to check the vowel.
 
Third, we need to check the vowel.
 
{{CodeBlock
 
{{CodeBlock
Line 34: Line 40:
 
}}<br />
 
}}<br />
  
 +
===Step 4:===
 
Then we need to use while loop to print the output.
 
Then we need to use while loop to print the output.
  
Line 46: Line 53:
 
}}<br />
 
}}<br />
  
Now we're done. You should write a complete program.
+
After this, we're done. You should now be able write a complete program that counts the number of vowels in a String given by the user.
 
+
|SolutionCode=
+
  
 +
==SolutionCode==
 +
{{OutputBlock
 +
|Code=
 
def main():
 
def main():
 
   done = False
 
   done = False
Line 68: Line 76:
  
 
       print '\nVowel count: %d\nVowel percentage: %d%%' % \
 
       print '\nVowel count: %d\nVowel percentage: %d%%' % \
             (vowelCount, vowelCount * 100 / leng(input))
+
             (vowelCount, vowelCount * 100 / len(input))
  
 
main()
 
main()
 
+
}}
     
+
+
 
+
 
}}
 
}}

Latest revision as of 03:21, 5 April 2012

COMP 1010 Home > Back to Extra Labs

Introduction

In this lab, you will learn how to use String methods in Python.

Problem

Write 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)) 

After this, we're done. You should now be able write a complete program that counts the number of vowels in a String given by the user.

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 / len(input))

main()