i = 0
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
s = input("请输入一串字符")
for i in s:
if 65 <= ord(s[i]) and ord(s[i]) <= 90 or 97 <= ord(s[i]) and ord(s[i]) <= 122:
sum1 += 1
elif ord(s[i]) == 32:
sum2 += 1
if 45 <= ord(s[i]) <= 78:
sum3 += 1
else:
sum4 += 1
print("字母有%d个"%sum1)
print("数字有%d个"%sum2)
print("空格有%d个"%sum3)
print("其他字符有%d个"%sum4)
遍历必须为整数string indices must be integers
最新推荐文章于 2024-08-16 18:55:06 发布