一个简单的统计字符/空格/其他的python程序:
#! /usr/bin/python
# -*- coding: UTF-8 -*-
import string
s = raw_input("请输入一个字符串:
")
letters = 0
space = 0
digit = 0
others = 0
i = 0
while i < len(s):
c = s[i]
i += 1
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print ('char = %d, space = %d, digit = %d, others = %d' % (letters, space, digit, others))
报错如下:
经查看,是print前面有个空格造成,删除空格后问题解决