|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
同样是输入'i love fishc.com','i love fishc.com1234567890', 本函数中最后两条语句的设置和直接fun('i love fishc.com','i love fishc.com1234567890' )的结果不一样,是为什么?
- def fun(*x):
- length=len(x)
- for i in range(length):
- num=0;alpha=0;space=0;other=0
- for each in x[i]:
- if each.isdigit():
- num+=1
- elif each.isalpha():
- alpha+=1
- elif each==' ':
- space+=1
- else:
- other+=1
-
- print('第 %d个字符串中数字%d个,字母%d个,空格%d个,其他字符%d个'%(i,num,alpha,space,other))
- temp=input('请输入字符串:')
- fun(temp)
复制代码
本帖最后由 凌九霄 于 2019-4-26 13:55 编辑
那你就输入的时候以特殊符号隔开,在调用统计函数前将其处理成元组,函数将元组参数去掉*号变成普通参数即可
- def fun(x):
- length = len(x)
- for i in range(length):
- num = 0;
- alpha = 0;
- space = 0;
- other = 0
- for each in x[ i ]:
- if each.isdigit():
- num += 1
- elif each.isalpha():
- alpha += 1
- elif each == ' ':
- space += 1
- else:
- other += 1
- print('第 %d个字符串中数字%d个,字母%d个,空格%d个,其他字符%d个' % (i, num, alpha, space, other))
- temp = input('请输入字符串(多个字符串以逗号隔开):')
- fun(temp.split(','))
复制代码
|
|