1. 程式人生 > >Python(66)_判斷使用者傳入的引數str中計算數字,字母,空格,以及其他的個數,並返回結果

Python(66)_判斷使用者傳入的引數str中計算數字,字母,空格,以及其他的個數,並返回結果

#-*-coding:utf-8-*-
'''
寫函式,判斷使用者傳入的引數str中計算數字,字母,空格,以及其他的個數,並返回結果
'''
content = input('>>>')

def func(s):
    num = 0
    alpha = 0
    space = 0
    others = 0
    dic ={
        'num':0,
        'alpha':0,
        'space':0,
        'other':0
    }
    for i in s:
        if i.isdigit():
            dic[
'num'] +=1 elif i.isalpha(): dic['alpha'] +=1 elif i.isspace(): dic['space'] +=1 else: dic['other'] +=1 return dic print(func(content))