当 this.txt 和本代码主语同一文件时
big_count=0
small_count=0
other_count=0
count=0
B="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
C="abcdefghijklmnopqrstyvwxyz"
d="0123456789"
with open(r"this.txt","r") as f:
while True:
fi =f.readline()
if not fi:
break
else:
for i in fi:
if i in B:
big_count +=1
elif i in C:
small_count +=1
elif i in d:
count +=1
else:
other_count +=1
print ("大写字母=",big_count,"小写字母=",small_count,"数字=",count,"其他字符=",other_count)
注:if not的用法:
None,False,0,空列表[],空字典{},空元祖(),都相当于false
not None 就相当于 ture
if not fi:
break
意味着 当fi为空 跳出本次循环