• 0

Newbie Python while loop help


Question

I picked up The Absolute Beginners Guide to Programming with Python and started to work through it. I'm stuck on a problem that deals with a while loop. You're suppose to count the vowels in an entered phrase twice (once with for loop and again with while loop), then output the numbers. I have the for loop working, but I appear to not be ablt to understand how to get it working with the while loop. I have included the code below. Any help will be appreciated.

count = 0

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

for letter in word:

if letter in "aeiou":

count +=1

else:

print "The number of vowels are: ", count

raw_input("Press the enter key to contniue")

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

count = 0

VOWEL = "aeiou"

while count < len(phrase):

for letter in word:

letter = phrase[count]

count = count +1

print "The number of vowels in your second phrase are: ",letter

Thanks,

Jackeye

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I am assuming this is all properly indented on your computer ;)

The while loop should be....

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

-numVowels = 0;

-count = 0

-vowel = "aeiou"

-while count < len(phrase):

-- if (phrase[count] in vowel):

--- numVowel = numVowels + 1

-print numVowels

EDIT: I had to show indentation with dashes.

Link to comment
Share on other sites

  • 0

Also, it seems your initial for loop has a bug.

Use "Test Phrase" as your test phrase. It will reply with a count of only 2, when the correct answer is 3. This is because your print statement is executed only if you don't find a vowel. :unsure:

Not sure if that is your intended effect.

Try this:

count = 0
word = raw_input("Please enter a phrase: ")
for letter in word:
  if letter in "aeiou":
	count +=1
  else:
	print "The number of vowels are: ", count

raw_input("Press the enter key to continue")

Oh, and the

 tag makes things easier to parse (and copy/paste)
Link to comment
Share on other sites

  • 0

Thanks for to you both for answering.

Thanks for bringing up the problem in the initial loop. I was so frustrated with the second, it slipped through.

Also, it seems your initial for loop has a bug.

Use "Test Phrase" as your test phrase. It will reply with a count of only 2, when the correct answer is 3. This is because your print statement is executed only if you don't find a vowel. :unsure:

Not sure if that is your intended effect.

Try this:

count = 0
word = raw_input("Please enter a phrase: ")
for letter in word:
  if letter in "aeiou":
	count +=1
  else:
	print "The number of vowels are: ", count

raw_input("Press the enter key to continue")

Oh, and the

 tag makes things easier to parse (and copy/paste)
Link to comment
Share on other sites

  • 0

Oops. My bad. I re-posted your code with indents and no modification. This is what I intended to post as a possible fix.

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("Press the enter key to continue")

Link to comment
Share on other sites

  • 0
Oops. My bad. I re-posted your code with indents and no modification. This is what I intended to post as a possible fix.
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("Press the enter key to continue")

hehe, I was about to comment saying just that

However, I think it needs to be :

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("Press the enter key to continue")

(note the indentation) The way you had it set up, it would print the number of vowels every time it finds one, instead of at the end.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.