|
发表于 2021-8-29 21:07:01
|
显示全部楼层
- def count(*param):
- print(f'传入的参数为param={param}')
- length = len(param) #这里的形参param为什么不是收集参数了,没有'*',从 *param -> param ?
- print(f'{param}中有{length}个元素')
- for i in range(length):
- letters = 0 # 这里的0是表示位置还是赋值?如果表示位置,为什么你?
- space = 0
- digit = 0
- others = 0
- for each in param[i]: #这里为什么要加上[i]?这里的[i]表示什么?
- print(f'each = 此时的字符为{each},i=param 里的第{i}个元素')
- if each.isalpha():
- letters += 1
- print(f'{each}是英文字母 letters加1个,此时letters={letters}')
- elif each.isdigit():
- digit += 1
- print(f'{each}是数字 digit加1个,此时digit={digit}')
- elif each == ' ':
- space += 1
- print(f'{each}是数字 space加1个,此时space={space}')
- else:
- others += 1
- print(f'{each}是其他字符 others 加1个,此时others ={others}')
- print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。'% (i+1,letters,digit,space,others)) #这里字符串的数量为什么是i+1?
- count('I love fishc.com.','I love you,you love me.')
复制代码 |
|