This Python program defines a function called count_letters() that takes a text as input and counts the number of letters present in the text. It iterates over each character in the text and increments a counter if the character is a letter. The program then tests the function by providing an input text and printing the count of letters.
def count_letters(text):
"""
This function counts the number of letters in a given text.
Args:
text (str): The input text.
Returns:
int: The number of letters in the text.
"""
letter_count = 0 # Initialize a counter for letters
# Iterate over each character in the text
for char in text:
if char.isalpha(): # Check if the character is a letter
letter_count += 1 # Increment the letter count
return letter_count
# Test the function
input_text = "Hello, World!"
num_letters = count_letters(input_text)
print("Number of letters:", num_letters)The count_letters() function initializes a counter variable letter_count to zero. It then iterates over each character in the input text using a for loop. For each character, it checks if the character is a letter using the isalpha() method. If the character is a letter, the letter_count is incremented by one.
In the provided example, the text “Hello, World!” is passed to the count_letters() function, and the result of 10 is printed, indicating that there are 10 letters in the text.