loops - Python code to count vowels -
loops - Python code to count vowels -
assume s
string of lower case characters.
write programme counts number of vowels contained in string s
. valid vowels are: 'a'
, 'e'
, 'i'
, 'o'
, , 'u'
. example, if s = 'azcbobobegghakl'
, programme should print:
number of vowels: 5
i have far
count = 0 vowels = 'a' or 'e' or 'i' or 'o' or 'u' vowels in s: count +=1 print ('number of vowels: ' + count)
can tell me wrong it?
a couple of problems. first, assignment vowels
doesn't think does:
>>> vowels = 'a' or 'e' or 'i' or 'o' or 'u' >>> vowels 'a'
python evaluates or
lazily; of predicates evaluates true
returned. non-empty sequences, including strings other ""
evaluate true
, 'a'
returned straight away.
second, when iterate on s
, ignore assignment anyway:
>>> vowels in "foo": print(vowels) f o o
for x in y:
assigns each item in iterable y
name x
in turn, assigned x
not longer accessible via name.
i think want is:
count = 0 vowels = set("aeiou") letter in s: if letter in vowels: count += 1
python loops
Comments
Post a Comment