dnker 发表于 2021-2-9 19:39:57

小白求助

def count(*param):
    length=len(param)
    for i in range(length):
      space=0
      numbers=0
      others=0
      letters=0
      for each in param:

            if each.isalpha():
                letters+=1
            elif each.isdigit():
                numbers+=1
            elif each==' ':
                space+=1
            else:
                others+=1
    print('第%d个字符串:空格有%d个,英文字母有%d个,数字有%d个,其他有%d个'%(i+1,space,letters,numbers,others))
count('I love fishc.com.','I love you, you love me.')


这里为什么只能打印第2个字符串:空格有5个,英文字母有17个,数字有0个,其他有2个,没有第一个呢,请大佬找出代码中的错误

Daniel_Zhang 发表于 2021-2-9 19:44:39

本帖最后由 Daniel_Zhang 于 2021-2-9 19:47 编辑

count 函数最后一行缩进问题

两个字符串存储在 param 内部,第一层 for 循环 param 的时候,你的 print 结果打印没有放到 for 里面,就只会打印最后一次的结果

def count(*param):
    length=len(param)
    for i in range(length):
      space=0
      numbers=0
      others=0
      letters=0
      for each in param:

            if each.isalpha():
                letters+=1
            elif each.isdigit():
                numbers+=1
            elif each==' ':
                space+=1
            else:
                others+=1
      print('第%d个字符串:空格有%d个,英文字母有%d个,数字有%d个,其他有%d个'%(i+1,space,letters,numbers,others))
count('I love fishc.com.','I love you, you love me.')

dnker 发表于 2021-2-9 19:54:03

Daniel_Zhang 发表于 2021-2-9 19:44
count 函数最后一行缩进问题

两个字符串存储在 param 内部,第一层 for 循环 param 的时候,你的 print...

感谢大佬,终究我还是粗心了

Daniel_Zhang 发表于 2021-2-9 19:59:54

dnker 发表于 2021-2-9 19:54
感谢大佬,终究我还是粗心了

多练习就好了,一开始学这个的话难免会出问题
页: [1]
查看完整版本: 小白求助