赞
踩
这题就是填一些简单代码,相信来认真看代码的找问题应该是第三个测试没有出来(空格数目对不上),我一开始也被摆了一道,后面仔细排查了下,发现题目里的space只针对空格(老坑了),所以把c.isspace()换成c==" "即可
这里就不上题目了(反正没啥用),直接上代码
- import string
-
- def read_file(file):
- """接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
- with open(file, 'r', encoding='utf-8') as f:
- return f.read()
-
- def classify_char(txt):
- """接收字符串为参数,依序返回大写字母、小写字母、数字、空格、和其他字符数量"""
- upper, lower, digit, space, other = 0, 0, 0, 0, 0
- for c in text:
- if c.isupper():
- upper+=1
- elif c.islower():
- lower+=1
- elif c.isdigit():
- digit+=1
- elif c==" ":
- space+=1
- else:
- other+=1
- #####
- return upper, lower, digit, space, other
-
- if __name__ == '__main__':
- filename = input() # 读入文件名
- text = read_file(filename) # 调用函数读文件中的内容为字符串
- classify = classify_char(text) # 调用函数分类统计字符数量
- print('大写字母{}个,小写字母{}个,数字{}个,空格{}个,其他{}个'.format(*classify))

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。