|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count(*param):
length=len(param)
for i in range(length):
space=0
numbers=0
others=0
letters=0
for each in param[i]:
if each.isalpha():
letters+=1
elif each.isdigit():
numbers+=1
elif each==' ':
space+=1
else:
others+=1
print('第%d个字符串:空格有%d个,英文字母有%d个,数字有%d个,其他有%d个'%(i+1,space,letters,numbers,others))
count('I love fishc.com.','I love you, you love me.')
这里为什么只能打印第2个字符串:空格有5个,英文字母有17个,数字有0个,其他有2个,没有第一个呢,请大佬找出代码中的错误
本帖最后由 Daniel_Zhang 于 2021-2-9 19:47 编辑
count 函数最后一行缩进问题
两个字符串存储在 param 内部,第一层 for 循环 param 的时候,你的 print 结果打印没有放到 for 里面,就只会打印最后一次的结果
- def count(*param):
- length=len(param)
- for i in range(length):
- space=0
- numbers=0
- others=0
- letters=0
- for each in param[i]:
- if each.isalpha():
- letters+=1
- elif each.isdigit():
- numbers+=1
- elif each==' ':
- space+=1
- else:
- others+=1
- print('第%d个字符串:空格有%d个,英文字母有%d个,数字有%d个,其他有%d个'%(i+1,space,letters,numbers,others))
- count('I love fishc.com.','I love you, you love me.')
复制代码
|
|