|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count(x):
a = len(list(x))
letter=0
numb=0
space=0
other=0
while a+1:
if list(x)[a].isalpha():
letter+=1
elif list(x)[a].isdigit():
numb+=1
elif list(x)[a].isspace():
space+=1
else:
other+=1
a-=1
print('字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (letter, numb, space, other))
count('i love fishc.com')
#为什么运行不了?
- def count(x):
- a = len(list(x))
- letter=0
- numb=0
- space=0
- other=0
- while a:
- if list(x)[a-1].isalpha():
- letter+=1
-
- elif list(x)[a-1].isdigit():
-
- numb+=1
-
- elif list(x)[a-1].isspace():
- space+=1
-
- else:
- other+=1
- a-=1
-
- print('字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (letter, numb, space, other))
- count('i love fishc.com')
复制代码
列表索引越界,把a全换成a-1就好了
同时,print结果的缩进不对,你这样会循环打印一堆,已修改
可以的话,给个最佳
|
|