#习题7:求一个字符串中的字母个数函数需判断传入参数的类型。必须使用ascii来判断是否字母

s = "I am a 12 years old boy!"

def letter_num(s):
    count=0
    if isinstance(s,str):
        for i in s:
            if (ord(i)>=97 and ord(i)<=121) or (ord(i)>=65 and ord(i)<=90):
                count+=1
    else:
        print("请传入一个字符串!")
        return -1
    return count


print(letter_num(s))

 

posted on 2019-10-05 10:48  腿短毛不多  阅读(172)  评论(0编辑  收藏  举报