|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在做20课的动动手时,我写了个程序如下:
s = '2345yurhgr4t5yhn vc '
digit = 0
alphabet = 0
blank = 0
other = 0
for i in s:
if i.isdigit():
digit += 1
elif i.isalpha():
alphabet += 1
print(i,end ='')
elif i == ' ':
blank += 1
else:
other += 1
print ('\n''该字符串共有%d个数字,%d个字母,%d个空格,%d个其它数字' % (digit, alphabet, blank, other))
这个程序可以正常运行并且得出正确结果:
yurhgrtyhnvc
该字符串共有6个数字,12个字母,2个空格,0个其它数字
但是当我把它改成函数的时候:
def string(*s):
digit = 0
alphabet = 0
blank = 0
other = 0
for i in s:
if i.isdigit():
digit += 1
elif i.isalpha():
alphabet += 1
print(i,end ='')
elif i == ' ':
blank += 1
else:
other += 1
print ('\n''该字符串共有%d个数字,%d个字母,%d个空格,%d个其它数字' % (digit, alphabet, blank, other))
string('2345yurhgr4t5yhn vc ')
却出错了,结果如下:
该字符串共有0个数字,0个字母,0个空格,1个其它数字
请问我这个函数该的哪里有问题么? |
|