ZCYDHHX 发表于 2018-1-11 10:34:55

零基础课后题19

def count(a):
    alptotal = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    number = '0123456789'
    qtzf = '.~!@#$%^&*()-=_+[]{}\|:";<>?,/'
   
    for each in a:
      b = a.count(alptotal)
      b = a.count(number)
      b = a.count(qtzf)
      
    return b

word = input('请输入字符串:')
c = count(word)
for each in c:
    print('第',each,'字符串包含',c,'个字幕',c,'个数字',c,'个其他字符')

这个运行报错,求问怎么回事

冬雪雪冬 发表于 2018-1-11 10:46:10

错误比较多:
1.input无法输入多个字符串
2.str.count得到的是子字符串在字符串中的数量。
我修改的程序:
def count(a):
    alptotal = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    number = '0123456789'
    qtzf = '.~!@#$%^&*()-=_+[]{}\|:";<>?,/'
    b =
    for each in a:
      if each in alptotal:
            b += 1
      elif each in number:
            b += 1
      elif each in qtzf:
            b += 1

      
    return b

word = input('请输入字符串:')
c = count(word)
print('字符串包含',c,'个字幕',c,'个数字',c,'个其他字符')

ZCYDHHX 发表于 2018-1-11 10:57:51

def count(*a):
    lenth = len(a)
    for i in range(length):
      letter = 0
      space = 0
      number = 0
      others = 0
      alptotal = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
      numbers = '0123456789'
      qtzf = '.~!@#$%^&*()-=_+[]{}\|:";<>?,/'
   
      for each in a:
            letter = a.count(alptotal)
            number = a.count(number)
            other = a.count(qtzf)
            space = a.count(' ')
      print('第',i,'字符串包含',letter,'个字母',number,'个数字',space,'个空格',other,'个其他字符'

word = input ('请输入字符串')
count(word)

ZCYDHHX 发表于 2018-1-11 11:00:28

我改了一下,调用函数怎么老是报错
页: [1]
查看完整版本: 零基础课后题19