-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathsub_17.py
More file actions
40 lines (32 loc) · 996 Bytes
/
sub_17.py
File metadata and controls
40 lines (32 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
'''
class Code:
content = '' # 代码
letters = 0 # 字母
space = 0 # 空格
digit = 0 # 数字
others = 0 # 字符
# 构造函数
def __init__(self, content):
self.content = content
# 分析代码
def analysis(self):
for c in self.content:
if c.isalpha():
self.letters += 1
elif c.isspace():
self.space += 1
elif c.isdigit():
self.digit += 1
else:
self.others += 1
# 展示结果
def showData(self):
form = '字母(%d) 空格(%d) 数字(%d) 其他字符(%d)'
data = (self.letters, self.space, self.digit, self.others)
print(form % data)
content = input('请输入一段代码:\n')
code = Code(content)
code.analysis() # 分析
code.showData() # 展示