-1

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?

Intro
  • 21
  • 3
  • 3
    Instead of `"a"` you have `sentence[char]` (which is one letter out of `sentence`) and instead of `apple` you have `"aeiouAEIOU"`. Is it now clear? – mkrieger1 Apr 01 '22 at 20:23
  • 2
    The lines you were asking about do not *find* the vowels. They test *if* something is a vowel (or a consonant). – mkrieger1 Apr 01 '22 at 20:25
  • 1
    Part of the confusion might be that `char` is not a character (i.e. a string of length 1), but an integer. You could try `print(char)` and `print(sentence[char])` each loop if that would help you understand. BTW, welcome to Stack Overflow! Check out the [tour] and [ask]. – wjandrea Apr 01 '22 at 20:30
  • 1
    BTW, you could simplify by using all lowercase: `if sentence[char].lower() in "aeiou"`. – wjandrea Apr 01 '22 at 20:33
  • Please change the title to reflect your changed question -- maybe something like "Why do I need to use the range of the length to loop over a string?" – wjandrea Apr 01 '22 at 20:46
  • Here's the relevant section in the official tutorial: [The `range()` Function](https://docs.python.org/3/tutorial/controlflow.html#the-range-function), under "To iterate over the indices of a sequence ...". And a relevant existing question: [Accessing the index in 'for' loops](/q/522563/4518341), especially [this answer](/a/28072982/4518341). – wjandrea Apr 01 '22 at 20:57

3 Answers3

1

Your program is going through sentence one letter at a time. For each letter (retreived by sentence[char]) it checks whether it is in the list of vowels (if yes, increment vowelCount) or in the list of consonants (if yes, increment consonantCount).

The form a in b for strings a and b checks whether a is contained somewhere as exact substring in b. So if a is just a single letter, it checks whether b contains the letter a anywhere.

Torben Klein
  • 2,943
  • 1
  • 19
  • 24
1

I suspect part of your confusion might arise because of the word "char." In the following code snippet, range(0, len(sentence)) generates numerical values. Thus, char is an index.

for char in range(0, len(sentence))

In other words, on the first iteration through the loop sentence[char] really looks something like sentence[0], or the first character in the sentence. If this character is in the string "aeiouAEIOU", the the boolean conditional in the loop returns TRUE

Note that if sentence[char] in "aeiouAEIOU" could be re-written like

if sentence[char] in set(['a','e','i','o','u','A','E','I','O','U'])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Evan W.
  • 342
  • 2
  • 10
0

First: "aeiouAEIOU" is a string, which can also be seen as a List of characters in this context.

So ['a', 'e', 'i', 'o'....'U'] is equivalent to this string representation above.

Second: The in operator in python checks if the element on the left side is "in" the list on the right side. So for example 1 in [1,2,3] would return TRUE while 10 in [1,2,3] would return FALSE. Or for characters, 'o' in "Foobar" would return TRUE while 'c' in "Foobar" would return FALSE

The rest is just (pseudocode) IF ... THEN increase a number, so you basically loop through every character of a sentence and increase two variables if the character is in the list of vovels or in the list of consonants. After the loop is finished, you present the final counts.

Jürgen Zornig
  • 1,174
  • 20
  • 48
  • 1
    *"a string, which is a fancy name for just a List of characters"* -- That's not really a useful way of thinking about it in a Python context, cause lists are mutable while strings aren't. Plus lists are containers while strings aren't. And strings have a ton of unique methods like `.upper()`. – wjandrea Apr 01 '22 at 20:49
  • @wjandrea Thanks, you are surely right on this point. I changed the sentence to better reflect what I meant in this context. – Jürgen Zornig Apr 04 '22 at 10:39