Exercício Python #077 - Contando vogais em Tupla

2 min read 4 months ago
Published on Aug 13, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will create a Python program that counts the vowels in a tuple of words. This exercise is part of a series designed to enhance your Python programming skills, particularly in handling data structures like tuples and strings.

Step 1: Create a Tuple of Words

Start by defining a tuple that contains several words. Remember, the words should not include any accents.

  • Open your Python environment (IDLE, Jupyter Notebook, etc.).
  • Define a tuple with words. For example:
words = ('banana', 'abacaxi', 'laranja', 'uva', 'manga')

Step 2: Define a Function to Count Vowels

Next, create a function that will iterate over each word in the tuple and count the vowels.

  • Define a function called count_vowels.
  • Use a for loop to go through each word.
  • Inside the loop, create a list to hold the vowels found in each word.

Here’s an example implementation:

def count_vowels(words):
    vowels = 'aeiou'
    for word in words:
        found_vowels = [letter for letter in word if letter in vowels]
        print(f'Vowels in "{word}": {found_vowels}')

Step 3: Call the Function

After defining your function, you need to call it and pass the tuple as an argument.

  • Below your function definition, call the function:
count_vowels(words)

Common Pitfalls to Avoid

  • Ensure that the words in your tuple do not contain any special characters or accents, as this exercise specifies.
  • Double-check your function for syntax errors, especially in the list comprehension.

Practical Tips

  • You can modify the tuple to include different words to test the function with various inputs.
  • Consider expanding the function to count the frequency of each vowel if you want to add more complexity.

Conclusion

In this tutorial, we created a simple Python program to count vowels in a tuple of words. You learned how to define a tuple, create a function to process the words, and print the vowels for each word. As a next step, try modifying the program to count the frequency of each vowel or include more words in the tuple. Happy coding!