|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count(*str1):
lenght = len(str1)
for i in range(lenght):
letters = 0
spase = 0
digit = 0
other = 0
for each in str1(i):
if each.isalpha() == True:
letters += 1
elif each.isdigit() == True:
digit += 1
elif each == ' ':
spase +=1
else:
other +=1
print('第%d个字符串中,英文字母有%d个,数字有%d个,空格有%d个,其他字符有%d个。'%(i+1,letters,digit,spase,other))
str1 = input('请输入字符串:')
count(str1)
请问一下各位大佬,这个会报错,因为收集参数收到的元组,要怎么把他变成字符串啊,我在第2行加了str1 = ' 'join(str1),还是会报错
不是join的问题()。
我把你的代码简单改了一下,建议再试一试。
- def count(*str1):
- lenght = len(str1)
- for i in range(lenght):
- letters = 0
- spase = 0
- digit = 0
- other = 0
- for each in str1[i]: #
- if each.isdigit():
- digit += 1
- elif each.isalpha():
- letters += 1
- elif each == ' ':
- spase += 1
- else:
- other +=1
-
- print('第%d个字符串中,英文字母有%d个,数字有%d个,空格有%d个,其他字符有%d个。'%(i+1,letters,digit,spase,other))
- str1 = input('请输入字符串:')
- count(str1)
复制代码
|
|