|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目为 统计出传入字符串参数的英文字母、空格、数字和其它字符的个数。
我的解答:
words = input('input your words here: ')
def azx(*words):
letters = 0
nums = 0
blanks = 0
others = 0
for each in words:
if each.isalpha() == True:
letters += 1
return letters
elif each.isdigit() == True:
nums += 1
return nums
elif each.isspace() == True:
blanks += 1
return blanks
else:
others += 1
return others
print('字符串共有:英文字母','%d' % letters,'个,数字','%d' % nums,'个,空格','%d' % blanks,'个,其他字符','%d' % others,'个。')
我能问一下大佬们我这个错在哪里了吗?学艺不精,勿怪!谢谢大佬们了!
帮你改了一下
- words = input('input your words here: ')
- def azx(words):
- letters = 0
- nums = 0
- blanks = 0
- others = 0
- for each in words:
- if each.isalpha() == True:
- letters += 1
- elif each.isdigit() == True:
- nums += 1
- elif each.isspace() == True:
- blanks += 1
- else:
- others += 1
- return letters, nums,blanks, others
- print('字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d'%azx(words))
复制代码
|
|