|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count(*param):
length = len(param)
for i in range(length):
letters = 0
space = 0
digit = 0
others = 0
for each in param[i]:
if each.isalpha():
letters += 1
elif each.isdigit():
digit += 1
elif each == " ":
space += 1
else:
others += 1
print('第%a 个字符串共有:英文字母 %m 个,数字 %c 个 ,空格%d,其他字符%e个'%(i+1,letters,digit,space,others))
为什么运行下去会是这种情况
count("12da")
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
count("12da")
File "E:/Python .project/检查字符串.py", line 17, in count
print('第%a 个字符串共有:英文字母 %m 个,数字 %c 个 ,空格%d,其他字符%e个。'%(i+1,letters,digit,space,others))
ValueError: unsupported format character 'm' (0x6d) at index 17
字符/字符串是 "%s",数字是 "%d",把代码改成这样:
def count(*param):
length = len(param)
for i in range(length):
letters = 0
space = 0
digit = 0
others = 0
for each in param[i]:
if each.isalpha():
letters += 1
elif each.isdigit():
digit += 1
elif each == " ":
space += 1
else:
others += 1
print('第 %d 个字符串共有:英文字母 %s 个,数字 %s 个 ,空格%d,其他字符%d个'%(i+1,letters,digit,space,others))
|
|