|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def count(*zifuchuan):
new = ''.join(zifuchuan)
length = len(new)
print(new)
for i in range(length):
alpha = 0
space = 0
digit = 0
others = 0
for each in new[i]:
if each.isalpha():
alpha +=1
elif each.isdigit():
digit +=1
elif each == ' ':
space +=1
else:
others +=1
print(alpha,digit,space,others)
我是想把输入的多参数合并为一个字符串,然后看看 数字 空格 字母是否在字符串中,到for i in range(length):这一步好像没啥问题,之后不知道哪里错了,比如我输入count('6576455','asd aeedf sdfs')
他只返回1 0 0 0
求大佬指正
def count(*zifuchuan):
new = ''.join(zifuchuan)
length = len(new)
print(new)
for i in range(length):
alpha = 0
space = 0
digit = 0
others = 0 # 这里的问题 for循环每执行一次你这里的值都要重新赋值一次
for each in new[i]:
if each.isalpha():
alpha +=1
elif each.isdigit():
digit +=1
elif each == ' ':
space +=1
else:
others +=1
print(alpha,digit,space,others)#所以你这输出的是最后一次的统计结果
修改
def count(*zifuchuan):
new = ''.join(zifuchuan)
length = len(new)
print(new)
alpha = 0
space = 0
digit = 0
others = 0 #放在这的话 下面的值才会把结果相加
for i in range(length):
for each in new[i]:
if each.isalpha():
alpha +=1
elif each.isdigit():
digit +=1
elif each == ' ':
space +=1
else:
others +=1
print(alpha,digit,space,others)
|
|