|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是这样的:
1. 编写一个函数,分别统计出传入字符串参数(可能不只一个参数)的英文字母、空格、数字和其它字符的个数。
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 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (i+1, letters, digit, space, others))
count('I love fishc.com.', 'I love you, you love me.')
我写的程序是这样的:
def count(*param):
length = len(param)
alpha = 0
space = 0
digit = 0
other = 0
for i in range(length):
for each in param[i]:
if each.isalpha() == 1:
alpha += 1
elif each.isdigit() == 1:
digit += 1
elif each.isspace() == 1:
space += 1
else:
other += 1
return(alpha,space,digit,other)
print(count('sad','sdsd'))
输出的结果是(3, 0, 0, 0)
请问这是怎么回事
函数定义的时候这种写法,是把多个输入变量都存在param这个参数中,比如本例中param[0]是'I love fishc.com.', param[1]是'I love you, you love me.'
第一个for循环是循环每个句子用的
第二for循环才是每一个句子里面具体的字符串
|
|