count = 0
vowels = ['a' , 'e' , 'i' ,'o' , 'u']
for char in s:
if char in vowels: # check if each char in your string is in your list of vowels
count += 1
print ('Number of vowels: ' + str(count)) # count is an integer so you need to cast it as a str
首先,请尝试以下操作:
使用你自己的循环。
也可以使用字符串格式:
有几个问题。首先,你分配给
vowels
的任务并不像你认为的那样:Python缓慢地计算
or
;只要任何谓词计算True
,它就会返回。非空序列,包括""
以外的字符串求值True
,因此'a'
将立即返回。其次,当您在
s
上迭代时,无论如何都会忽略该赋值:for x in y:
依次将iterabley
中的每个项分配给名称x
,因此以前分配给x
的任何内容都无法再通过该名称访问。我想你想要的是:
相关问题 更多 >
编程相关推荐