Advertisement
Python253

vowel_count

Mar 3rd, 2024 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: vowel_count.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Vowel Count Script
  8.  
  9. This script reads text from a specified file in the same directory and counts
  10. the number of vowels in the text. It utilizes a function, count_vowels, to
  11. perform the vowel counting operation.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Save the text file in the same directory as the script.
  18. 2. Replace 'texty.txt' with the actual file name in the script.
  19. 3. Run the script.
  20. 4. The script will display the number of vowels in the text.
  21. """
  22.  
  23. def count_vowels(text):
  24.     """Count the number of vowels in a text.
  25.  
  26.    Args:
  27.        text (str): Text.
  28.  
  29.    Returns:
  30.        int: Number of vowels in the text.
  31.  
  32.    """
  33.     vowels = ['a', 'e', 'i', 'o', 'u']
  34.     return sum(text.lower().count(vowel) for vowel in vowels)
  35.  
  36. def read_text_from_file(file_path):
  37.     """Read text from a file.
  38.  
  39.    Args:
  40.        file_path (str): Path to the file.
  41.  
  42.    Returns:
  43.        str: Text read from the file.
  44.  
  45.    """
  46.     with open(file_path, 'r', encoding='utf-8') as file:
  47.         return file.read()
  48.  
  49. def main():
  50.     # Specify the file path
  51.     file_path = 'texty.txt'  # Replace 'texty.txt' with the actual file name
  52.  
  53.     # Read text from the file
  54.     text = read_text_from_file(file_path)
  55.  
  56.     # Count vowels
  57.     vowel_count = count_vowels(text)
  58.  
  59.     # Display the result
  60.     print(f"Number of vowels in the text: {vowel_count}")
  61.  
  62. if __name__ == "__main__":
  63.     main()
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement