我想问下大家 for循环中为啥我在last 后面加1才是正确结果啊,不加1的话少了一个
def edg(x):english=0
number=0
block=0
others=0
length=len(x)
last=length-1
str1=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
str2=['0','1','2','3','4','5','6','7','8','9']
str3=[' ']
for each in range(last+1):
if x in str1:
english+=1
continue
elif x in str2:
number+=1
continue
elif x in str3:
block+=1
continue
else :
others+=1
continue
return (english,number,block,others)
x=input('请输入字符串')
print(edg(x)) range含头不含尾 据我观察,但是不一定正确,(可以这样记忆)
python计数是从0开始计的,为了让切片数据无缝连接起来,也即
a= 从0计起8为第7位,
a = a+a+a
计算数据位置,前面多计算了个0,后面少计算了个数,加上即可
你好好研究一下range函数 会明白的 不算太难应用
页:
[1]