2016年10月8日星期六

统计元音字母

统计元音字母——输入一个字符串,统计处其中元音字母的数量。更复杂点的话统计出每个元音字母的数量。

#coding=utf8
#!/usr/bin/python
mystr='bananaaeiousiou'
yuanyin=('a','e','i','o','u')
num=0
for s in mystr:
    if s in yuanyin:
        num+=1
print len(mystr)
print num

numa=0
nume=0
numi=0
numo=0
numu=0

for s in mystr:
    if s == 'a':
        numa+=1
    elif s == 'e':
        nume+=1
    elif s == 'i':
        numi+=1
    elif s == 'o':
        numo+=1
    elif s == 'u':
        numu+=1
print 'a is :'+str(numa)
print "a is %d" %numa
print 'e is :'+str(nume)
print 'i is :'+str(numi)
print 'o is :'+str(numo)
print 'u is :'+str(numu)

没有评论:

发表评论