Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- PURPOSE: Count the vowels (both uppercase and lowercase) in an entire file.
- This program has no user input. The provided main uses the function you will
- write as a demo.
- HINT: This requires both a loop for going of the lines of text in the file and
- a loop to go over the characters within each line.
- AUTHOR: Natalie Preiss
- '''
- def count_vowels(filename):
- """
- Counts and returns the number of vowels in the file with the given name. Since the file could
- be very large, the file must be iterated line-by-line. You cannot use any of the read functions.
- """
- # add function here
- f = open(filename)
- #Our list that will contain the vowels
- vowels = ['a','e','i','o','u']
- #Variable that will count the vowels
- countVowels = 0
- #Read line from the file
- line = f.readline()
- #While there are lines present in the file keep reading
- while line:
- #For each character (in lower case) of line check if it is in our list of vowels
- for character in line:
- if character.lower() in vowels:
- countVowels += 1
- #Read the next line
- line = f.readline()
- #Return the number of vowels
- return countVowels
- def main():
- vowel_count = count_vowels('alice.txt')
- print("There are", vowel_count, "in Alice in Wonderland")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement