如果输出判断类型
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:
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)
alpha = 0
space = 0
digit = 0
others = 0
for i in range(length):
if new.isalpha():
alpha +=1
elif new.isdigit():
digit +=1
elif new == ' ':
space +=1
else:
others +=1
print(alpha,digit,space,others)
count('6576455','asd aeedf sdfs') 本帖最后由 傻眼貓咪 于 2021-9-13 17:06 编辑
{:5_109:}{:5_109:}你的 alpha = 0,space = 0,digit = 0,others = 0 必須放在 for 循環外面,不然每次更新結果後又恢復 0{:5_109:}{:5_109:}
我的代碼:(供參考)
def strInfo(*strs: str) -> dict():
res = {}.fromkeys(['alpha', 'space', 'digit', 'others'], 0) # 創建字典,增加輸出可讀性
for char in ''.join(strs): # 直接遍歷,無需再創建新列表
if char.isalpha(): res['alpha'] += 1
elif char.isdigit(): res['digit'] += 1
elif char == ' ': res['space'] += 1
else: res['others'] += 1
return res.items() # 返回字典裡的元素
print(*strInfo('6576455','asd aeedf sdfs')) # 打印('alpha', 12) ('space', 2) ('digit', 7) ('others', 0){:7_146:} {:7_146:} {:7_146:} 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:
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:
if each.isalpha():
alpha +=1
elif each.isdigit():
digit +=1
elif each == ' ':
space +=1
else:
others +=1
print(alpha,digit,space,others)
页:
[1]