Define a function called
count_engcons()
which takes a string and returns the number of consonants in the string (uppercase or lowercase). For this problem, you may consider only letters in the English language alphabet only. Also, for this problem "Y" is considered a consonant (...not a vowel!). So for examplecount_engcons("Tessellated?")
should return7
, andcount_engcons("Aeiou!")
should return0
. You must use afor
loop, and you are not allowed to use the.count()
method on this problem.
我试过这个:
def count_engcons(x):
vowels = ("aeiou")
count = 0
for count_engcons in text:
if not count_engcons in vowels:
count += 1
return x
但是,它会导致错误
谢谢Jornsharpe的否决票
您正在检查一个字符是否不是元音,因此对于诸如
!
或?
之类的字符,它会给出不好的结果。您还试图使用不同的变量名(x
和text
)访问字符串,这是没有意义的相关问题 更多 >
编程相关推荐