[Tutor] (no subject)

Alan Gauld alan.gauld at btinternet.com
Tue Sep 8 19:45:32 CEST 2009


<shellcom3 at juno.com> wrote in message 

Please specify a meaningful subject line, it makes reading messages 
in a threaded mailtool or newsreader much easier.

> #demo for loop, and while loop
> 
> phrase = raw_input("enter your phrase:")
> VOWELS = "aeiou"
> number = 0
> 
> for letter in phrase:
>    if letter.lower() in VOWELS:
>        number = number +1
>    else:
>        pass

You don't need the else if its only a pass statement.

>    print "Number of vowels =",number
> 
> raw_input("Press the enter key to continue.")
> index = 0
> while index < len(phrase):
>    if phrase[index] in VOWELS:
>         index = index +1
>         print "number of vowels" ,index

The index is the position in the string not the count of vowels.
But you are trying to use the same variable for both. Thats a bad idea.

Because you only increment the position if thre is a vowel you 
get stuck if any letter is not a vowel. You need a new count variable
and keep index just for iterating in the loop..

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list