Exercise
Function To Calculate DV
Objective
Develop a Python program that defines a function to count the number of numeric digits and vowels in a given string. The function should take three arguments: the string to analyze, a variable for the digit count, and a variable for the vowel count. This function, named CountDV, can be used as follows:
CountDV('This is the phrase 12', amount_of_digits, amount_of_vowels)
In this example, amount_of_digits will return 2, and amount_of_vowels will return 5.
Example Python Exercise
Show Python Code
# Function to count the number of numeric digits and vowels in a given string
def CountDV(input_string, digit_count, vowel_count):
# Define the vowels
vowels = "aeiouAEIOU"
# Initialize the counts to 0
digit_count = 0
vowel_count = 0
# Iterate over each character in the input string
for char in input_string:
# Check if the character is a digit
if char.isdigit():
digit_count += 1
# Check if the character is a vowel
elif char in vowels:
vowel_count += 1
# Return the counts as a tuple
return digit_count, vowel_count
# Example usage of the CountDV function
def main():
# Define initial counts
amount_of_digits = 0
amount_of_vowels = 0
# Call the CountDV function with a string and count variables
amount_of_digits, amount_of_vowels = CountDV('This is the phrase 12', amount_of_digits, amount_of_vowels)
# Print the results
print(f"Amount of digits: {amount_of_digits}")
print(f"Amount of vowels: {amount_of_vowels}")
# Run the main function if the script is executed directly
if __name__ == "__main__":
main()
Output
This is the phrase 12
Amount of digits: 2
Amount of vowels: 5
Share this Python Exercise