Advertisement
homeworkhelp111

Count_Vowels

Nov 12th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. '''
  2. PURPOSE: Count the vowels (both uppercase and lowercase) in an entire file.
  3.  
  4. This program has no user input. The provided main uses the function you will
  5. write as a demo.
  6.  
  7. HINT: This requires both a loop for going of the lines of text in the file and
  8. a loop to go over the characters within each line.
  9.  
  10. AUTHOR: Natalie Preiss
  11. '''
  12.  
  13.  
  14. def count_vowels(filename):
  15.     """
  16.    Counts and returns the number of vowels in the file with the given name. Since the file could
  17.    be very large, the file must be iterated line-by-line. You cannot use any of the read functions.
  18.    """
  19.     # add function here
  20.     f = open(filename)
  21.  
  22.     #Our list that will contain the vowels
  23.     vowels = ['a','e','i','o','u']
  24.  
  25.     #Variable that will count the vowels
  26.     countVowels = 0
  27.  
  28.     #Read line from the file
  29.     line = f.readline()
  30.  
  31.     #While there are lines present in the file keep reading
  32.     while line:
  33.         #For each character (in lower case) of line check if it is in our list of vowels
  34.         for character in line:
  35.             if character.lower() in vowels:
  36.                 countVowels += 1
  37.         #Read the next line
  38.         line = f.readline()
  39.  
  40.     #Return the number of vowels
  41.     return countVowels
  42.  
  43. def main():
  44.     vowel_count = count_vowels('alice.txt')
  45.     print("There are", vowel_count, "in Alice in Wonderland")
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     main()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement