本题要求编写程序,输入一行字符,统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。 输入格式: 输入在一行中给出若干字符,最后一个回车表示输入结束,不算在内。 输出格式: 在一行内按照 blank = 空格个数, digit = 数字字符个数, other = 其他字符个数 的格式输出。请注意,等号的左右各有一个空格,逗号后有一个空格。 输入样例: 在这里给出一组输入。例如: reold 12 or 45t 输出样例: 在这里给出相应的输出。例如: blank = 3, digit = 4, other = 8
时间: 2023-04-28 18:02:28 浏览: 50
代码如下:
```python
s = input() # 输入一行字符
blank = # 空格个数
digit = # 数字字符个数
other = # 其他字符个数
for c in s:
if c.isdigit(): # 判断是否为数字字符
digit += 1
elif c.isspace(): # 判断是否为空格
blank += 1
else:
other += 1
print("blank = {}, digit = {}, other = {}".format(blank, digit, other)) # 输出结果
```
使用 switch 语句的代码如下:
```python
s = input() # 输入一行字符
blank = # 空格个数
digit = # 数字字符个数
other = # 其他字符个数
for c in s:
switch = {
c.isdigit(): 1,
c.isspace(): 2,
}.get(True, 3) # 判断字符类型,数字字符为1,空格为2,其他为3
if switch == 1:
digit += 1
elif switch == 2:
blank += 1
else:
other += 1
print("blank = {}, digit = {}, other = {}".format(blank, digit, other)) # 输出结果
```