Function to Calculate DV - Python Programming Exercise

In this exercise, you will 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 exercise is perfect for practicing function definition, string manipulation, and counting operations in Python. By implementing this function, you will gain hands-on experience in handling function definitions, string manipulation, and counting operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 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

 Copy 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

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.