For an assignment, I needed to make a program that counts the vowels and consonants in a string using for i in range(0, len(str)):
I put this program together using what I learned, but I can't really wrap my head around why it works.
vowelCount = 0
consonantCount = 0
sentence = input("Enter your sentence: ")
for char in range(0, len(sentence)):
if sentence[char] in "aeiouAEIOU":
vowelCount += 1
if sentence[char] in "bcdfghjklmnpqrstvwxyBCDFGHJKLMNPQRSTVWXYZ":
consonantCount += 1
print("There are", vowelCount, "vowels")
print("There are", consonantCount, "consonants")
Why am I getting the range of the length of the sentence?
Here's an alternative program I wrote without the range.
vowelCount = 0
consonantCount = 0
sentence = input("Enter your sentence: ")
for i in sentence:
if i in "aeiouAEIOU":
vowelCount += 1
if i in "bcdfghjklmnpqrstvwxyBCDFGHJKLMNPQRSTVWXYZ":
consonantCount += 1
print("There are", vowelCount, "vowels")
print("There are", consonantCount, "consonants")
Why do I need to use sentence[char] in the range version? Why the brackets?