Bird
0
0

Write code to count how many vowels are in the string sentence. Which code correctly does this?

hard📝 Application Q15 of 15
Python - For Loop
Write code to count how many vowels are in the string sentence. Which code correctly does this?
Acount = 0 for ch in sentence: if ch in 'aeiouAEIOU': count += 1 print(count)
Bcount = 0 for i in range(len(sentence)): if sentence[i] == 'aeiou': count += 1 print(count)
Ccount = 0 for ch in sentence: if ch == 'a' or 'e' or 'i' or 'o' or 'u': count += 1 print(count)
Dcount = 0 for ch in sentence: if ch in ['a','e','i','o','u']: count = count + 1 print(count)
Step-by-Step Solution
Solution:
  1. Step 1: Check vowel membership correctly

    count = 0 for ch in sentence: if ch in 'aeiouAEIOU': count += 1 print(count) checks if each character is in the string of vowels (both lowercase and uppercase), which is correct.
  2. Step 2: Verify counting logic

    count = 0 for ch in sentence: if ch in 'aeiouAEIOU': count += 1 print(count) increments count correctly when a vowel is found and prints the total count.
  3. Final Answer:

    count = 0 for ch in sentence: if ch in 'aeiouAEIOU': count += 1 print(count) -> Option A
  4. Quick Check:

    Use 'in' with vowel string and increment count [OK]
Quick Trick: Use 'if ch in "aeiouAEIOU"' to check vowels [OK]
Common Mistakes:
MISTAKES
  • Using '==' to compare with multiple vowels
  • Checking membership incorrectly with list without uppercase
  • Not incrementing count properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes