0

I got first part by creating it using for loop. But got stuck on while loop. Would you see what I'm doing wrong.


# Calculating number of vowels using for loop

count = 0
word = raw_input("Please enter a phrase: ")
for letter in word:
if letter in "aeiou":
count +=1

print "The number of vowels are: ", count

raw_input("\n\nPress the enter key to continue\n\n")


# Calculatint number of vowels using while loop

word = raw_input("Please enter a second phrase: ")
numVowels = 0
count = 0
vowel = "aeiou"
while count < len(word):
if (word[count] in vowel):
numVowels = numVowels + 1

print numVowels

3
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by jlm699
0
  1. U are trying to compare the vowels list as a single string. Each vowel is a character and not a string !!!
  2. U can compare it either as a string/character
  3. Also make sure u ignore the case during comparisions (u can lower the case before comparision otherwise.. i have incorporated in the code below)

        char[] vowelsList = new char[5]{'a', 'e', 'i', 'o', 'u'};
        string input = "I am the Big Boss !!!";
        char[] analyseInput = input.ToLower().ToCharArray();
    
        int vowelCount = 0;
    
        foreach ( char vowel in vowelsList)
        {
            for (int i = 0; i < analyseInput.Length; i++)
            {
                if (analyseInput[i].Equals(vowel))
                {
                    vowelCount++;
                }
            }
        }
    

I got first part by creating it using for loop. But got stuck on while loop. Would you see what I'm doing wrong.

# Calculating number of vowels using for loop

count = 0
word = raw_input("Please enter a phrase: ")
for letter in word:
    if letter in "aeiou":
        count +=1

print "The number of vowels are: ", count

raw_input("\n\nPress the enter key to continue\n\n")


# Calculatint number of vowels using while loop



word = raw_input("Please enter a second phrase: ")
numVowels = 0
count = 0
vowel = "aeiou"
while count < len(word):
    if (word[count] in vowel):
        numVowels = numVowels + 1

print numVowels

Edited by pyTony: fixed formatting

0

or may be .. like this :-)

string input = "I am the Big Bossa";
string[] vowelsList = new string[6]{"a", "e", "i", "o", "u"};
int vowelCount = 0;

foreach (string vowel in vowelsList)
{
    int startIndex = 0;
    do
    {
        startIndex = input.IndexOf(vowel, startIndex + 1, StringComparison.InvariantCultureIgnoreCase);
        if (startIndex != -1)
        {
            vowelCount++;
        }
    } while (startIndex != -1);
}

Edited by pyTony: fixed formatting

0

Would you see what I'm doing wrong.

You've got an infinite loop at the end there...

while count < len(word):
    if (word[count] in vowel):
        numVowels = numVowels + 1

You see? You never update the count variable, so it'll always be less than the length of word (ie, infinite loop).

In the future please post your code surrounded by code brackets so that it's easier to read
[code=python] # Code in here

[/code]

And like that C-loving guy suggested, it would help if you could ignore caps vowels by doing something like this:

word = raw_input("Enter X: ")
word = word.lower()

lower() is a string method that returns the lower-case representation of the string.

0

Asssingment is:
The while loop will need a counter to count how many times the while loop runs. This counter will go up to the length of the word. Another counter will be needed to count the vowels. In order to get each letter, use the counter for the while loop as an index to the word such as word[counter].

0

I reiterate: you never update the variable named count in your while loop, so it's stuck in an infinite loop. You'll need to increase it on every iteration, ie count += 1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.