[Tutor] (no subject)

Christian Witts cwitts at compuscan.co.za
Tue Sep 8 16:51:05 CEST 2009


shellcom3 at juno.com wrote:
> I want to write a code that allows me to input a phrase and calculate the number of vowels twice. once with the for loop and second with the while loop.I can get the for loop to work but, not the while loop. this is my code and response.
>
>
> #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
>     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
>
>
>
> enter your phrase:tell me your name
> Number of vowels = 0
> Number of vowels = 1
> Number of vowels = 1
> Number of vowels = 1
> Number of vowels = 1
> Number of vowels = 1
> Number of vowels = 2
> Number of vowels = 2
> Number of vowels = 2
> Number of vowels = 3
> Number of vowels = 4
> Number of vowels = 4
> Number of vowels = 4
> Number of vowels = 4
> Number of vowels = 5
> Number of vowels = 5
> Number of vowels = 6
> Press the enter key to continue.
>
>
>
> ____________________________________________________________
> Best Weight Loss Program - Click Here!
> http://thirdpartyoffers.juno.com/TGL2141/fc/BLSrjpTFoYcWEqYCcbHEaFPTq8jdS8V2cC4YnTjQIbcZlB9fvaKI0BwmgT2/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>   
Your while loop will continue indefinitely if the phrase is not 
completely vowels.  What you should do is remember to increment the 
value of index regardless of if the letter is a vowel of not:

VOWELS = 'aeiouAEIOU'
cnt = 0
index = 0
while index < len(phrase):
    if phrase[index] in VOWELS:
        cnt += 1
        print 'Number of Vowels = %s' %cnt
    index += 1

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list