假设s是一个小写字符串。
编写一个程序,计算字符串s中包含的元音数量。有效的元音包括:' a','' i',' o'和' u'。例如,如果s =' azcbobobegghakl',您的程序应打印:
元音数量:5
我想出了这段代码,但我可能会错过一些我无法理解的东西
s = 'fddjhkloeavhkiyaeio'
vowels = ['a','e','i','o','u']
count = 0
for i in s:
if i in vowels:
count = count + 1
print("Number of vowels: " , str(count))
这是错误 元音数:8
*错误:预计6,得到8. *
答案 0 :(得分:1)
上面有@castis首先指出的问题。您的打印声明位置错误。如果你把它放在for循环中,这意味着它会在每次for循环迭代时打印,所以把它带回2个缩进。像这样:
s = 'fddjhkloeavhkiyaeio'
vowels = ['a','e','i','o','u']
count = 0
for i in s:
if i in vowels:
count = count + 1
print("Number of vowels: " , str(count))