I absolutely can't figure this problem out, can anyone please help?

Write a program that asks for a phrase and then calculates and displays the number of vowels in the phrase, twice. The first time, your program should calculate the number of vowels in the phrase using a for loop. The second time, your program should use a while loop.

I have been stuck on this for two hours now! Please help!

Well first, break down the problem into sections.

1) Get input phrase
2) Calculate number of vowels in phrase with a for loop
3) Calculate number of vowels in phrase with a while loop
4) Output

If needed, you can put an output step after step 2.

Now tackle each one at a time.

- Get string input and put it into a string variable
- Step through the string and compare each letter with each type of vowel using the index operator (ex: mystring[3]) and keep count of the number of each vowel you encounter.
- Print out the data in a readable format

You can accomplish this with both the for loop and the while loop.

Hope that helps.

I am just stuck putting all of this into code. This is what I have done so far...

phrase = raw_input("Please enter a phrase:")

length = len (phrase)

VOWELS = "aeiou"

vowel_total = 0

Okay. That's a good start.

You can use the "in" operator in python to see if a character is in a string. Use this see if a certain letter is in the vowels string.

Here is my example program:

phrase = raw_input("Please enter a phrase: ")
length = len(phrase)
VOWELS = "aeiou"
vowel_total = 0

for letter in phrase:
    if letter in VOWELS:
        vowel_total += 1

print "There are " + str(vowel_total) + " vowels in the phrase '" + phrase + "'."

Example input/output:

>>>
Please enter a phrase: Programming is fun.
There are 5 vowels in the phrase 'Programming is fun.'.

Here, "for letter in phrase" is the beginning of the for loop. It goes through the phrase letter by letter and executes whatever is in the for loop (indented below that first line) with "letter" being the variable representing the current letter. Does that make sense?

Note: This code only counts lowercase vowels. You'll need to make a change to it if you want to catch all vowels.

That is what I am stuck on. How do I do both Upper and lower case? How do I manage to add both totals together?

Remember how I said that "if letter in VOWELS:" checks if that certain character letter is in the string VOWELS? If you want to check if it's uppercase as well, just add the uppercase vowels to that VOWELS string.

VOWELS = "aeiouAEIOU"

Edited 6 Years Ago by Mouche: n/a

I managed to get the upper and lower to add together by doing this...

for letter in phrase:
if letter in VOWELS:
vowel_total += 1

for letter in phrase:
if letter in VOWELS.upper():
vowel_total += 1

How do I go about with the While loop?

Perfect. That works. You can simplify it by putting those two together:

if letter in VOWELS or letter in VOWELS.upper():
        vowel_total += 1

To do it the while loop way, you can have an index count variable that starts at zero and have the while loop go "while the index count is less than the string length". Then inside the while loop you can do that same check above and then increment the index variable.

I got it all together but am hitting an issue. Here is my coding, any idea what is wrong?

while vowel_total < length:
if letter in VOWELS or letter in VOWELS.upper():
vowel_total += 1
print "There is a total of",vowel_total,"vowels in the phrase that you entered, according"
print "to the While Loop."

Note: Please use CODE tags, which can be found in the message editing toolbar. It makes code much easier to read.

The first line of your while statement doesn't really make sense. Overall the loop says "Check to see if letter is in VOWELS while vowel_total is less than the length." You want to use the while loop to go through the letters of the word.

Try adapting this code, which goes through each letter of a string, to your project.

phrase = raw_input("Enter your phrase: ")
phrase_length = len(phrase)
index = 0

while index < phrase_length:
    print phrase[index]
    index += 1

You want to replace the print statement with your comparison where letter, the current letter, will instead be phrase[index], which represents the current letter in the loop.

Does that make sense?

With the index, you can get a letter in the phrase.

phrase[0] is the first letter of the phrase
phrase[1] is the second letter of the phrase

You can have phrase[index] represent the current letter and then use a while loop to go through all the letters in the current phrase.

Use phrase[index] as you used letter before.

if phrase[index] in VOWELS or phrase[index] in VOWELS.upper():
    vowels_count += 1
phrase = raw_input("Please enter a phrase: ")
length = len(phrase)
VOWELS = "aeiou"
vowel_total = 0

for letter in phrase:
     if letter in VOWELS:
      vowel_total += 1

for letter in phrase:
    if letter in VOWELS.upper():
        vowel_total += 1

print "There is a total of",vowel_total,"vowels in the phrase that you entered."

raw_input("\nPress the enter key to continue to see what the While Loop comes up with.")

phrase = raw_input("Enter your phrase: ")
phrase_length = len(phrase)
index = 0

while index < phrase_length:
     print phrase[index]
     index += 1
if phrase[index] in VOWELS or phrase[index] in VOWELS.upper():
      vowel_total += 1

I keep getting errors and have no idea what is wrong with it. The first part (for loop) is fine, no problems, I can't figure out what's wrong with the while loop.

There are three issues with your code.

The first is that you don't reset vowel_total for the second part.

The second problem is that your "if phrase[index] in..." is not in the while loop. It should where the print statement is and you can get rid of the print statement. That was just an example I was showing you.

Finally, you need to copy that print statement that says how many vowels are in the phrase from the first section to the second one.

phrase = raw_input("Enter your phrase: ")
length = len(phrase)
index += 1
vowel_total = 0

while index < length:
     vowel_total += 1
     print "There is a total of",vowel_total,"vowels in the phrase that you entered, according to the While Loop."

This is what I have for the While Loop portion. I am not getting errors anymore, it is just not displaying any vowels after entering the phrase (While loop).

I don't think you're quite getting the logic of the loop. You start with the index as 0. That way phrase[index] represents the first letter of the phrase, which is a string. phrase[len(phrase) - 1] represents the last letter of the phrase. Using the following logic, you can go through the string Hello one letter at a time and do something with that letter:

phrase = 'Hello'
index = 0
phrase_length = len(phrase)
while index < phrase_length:
    ## Do something here 
    index += 1

In your case, you want to do the comparison in the loop, not print something. The print statement goes after the loop.

Also, you need to reset the vowel_count for the second section because you want to start from 0 again.

With the fixes:

phrase = raw_input("Please enter a phrase: ")
length = len(phrase)
VOWELS = "aeiou"
vowel_total = 0

for letter in phrase:
     if letter in VOWELS:
      vowel_total += 1

for letter in phrase:
    if letter in VOWELS.upper():
        vowel_total += 1

print "There is a total of",vowel_total,"vowels in the phrase that you entered."

raw_input("\nPress the enter key to continue to see what the While Loop comes up with.")

phrase = raw_input("Enter your phrase: ")
phrase_length = len(phrase)
index = 0
vowel_count = 0

while index < phrase_length:
    if phrase[index] in VOWELS or phrase[index] in VOWELS.upper():
        vowel_total += 1
    index += 1

print "There is a total of",vowel_total,"vowels in the phrase that you entered."

Edited 6 Years Ago by Mouche: n/a

Wow I get it now. I actually did have to reset the vowel_total on the second part otherwise it continued to add onto the results from the for loop. Thank you so much! I have been workin on this thing for literally three or four hours! I extremely appreciate it!

This question has already been answered. Start a new discussion instead.