|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:1. 编写一个函数,分别统计出传入字符串参数(可能不只一个参数)的英文字母、空格、数字和其它字符的个数。
我的代码如下:
def tongji(str1):
zimu = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
shuzi = '0123456789'
space = ' '
others = '+-*/.'
z = 0
sz = 0
sp = 0
ot = 0
i = len(str1)
t = 0
while t <= i-1:
if str1[t] in zimu :
z += 1
if str1[t] in shuzi:
sz += 1
if str1[t] in space:
sp += 1
if str1[t] in others:
ot += 1
t += 1
print('第1个字符串共有:英文字母',z,'个,数字',sz,'个,空格',sp,'个,其他字符',ot,'个')
我输入了这个字符串:I love fishc.com.
问题是只识别出了13个字母,空格和特殊字符都不能识别。问题出在哪?
缩进错了:
- def tongji(str1):
- zimu = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- shuzi = '0123456789'
- space = ' '
- others = '+-*/.'
- z = 0
- sz = 0
- sp = 0
- ot = 0
- i = len(str1)
- t = 0
- while t <= i-1:
- if str1[t] in zimu :
- z += 1
- if str1[t] in shuzi:
- sz += 1
- if str1[t] in space:
- sp += 1
- if str1[t] in others:
- ot += 1
- t += 1
- print('第1个字符串共有:英文字母',z,'个,数字',sz,'个,空格',sp,'个,其他字符',ot,'个')
复制代码
|
|