foxdai 发表于 2020-3-28 22:08:45

Python L19 函数1 回文联 字符计数

本帖最后由 foxdai 于 2020-4-8 18:23 编辑

#回文联函数
def Laihui():
        '回文联即用回文形式写成的对联,即可顺读,也可倒读。例如:上海自来水来自海上'
        sentence = input("请输入一句话:")
        if len(sentence) % 2 == 0 or len(sentence) <3:
                LaiHui = False
        else:
                length = len(sentence)
                Laihui = False
                for i in range(int((length-1)/2)):
                        if sentence == sentence:
                                LaiHui = True
                        else:
                                LaiHui = False
                                break
        if LaiHui:
                print('是回文联')
        else:
                print('不是回文联')

#计数,含子函数count1(x)       
def count(*params):
    cellcounts = len(params)
    for i in range(cellcounts):
            print('第',i+1,' 个字符串共有:',end='')
            count1(params)

#计数的子函数
def count1(x):
#判断为整数,则转为字符
      if type(x) == int:
            x = str(x)
#计数            
      length = len(x)
      alphas = nums = spaces = others = 0
      for i in range(length):
                if x.isalpha():
                        alphas += 1
                else:
                        if x.isnumeric():
                              nums += 1
                        else:
                              if x.isspace():
                                        spaces += 1
                              else:
                                        others += 1
      print('英文字母 ','%2d' % alphas,' 个,数字 ','%2d' % nums,' 个,空格 ',\
            '%2d' % spaces,'个,其他字符 ','%2d' % others,'个。')
      
页: [1]
查看完整版本: Python L19 函数1 回文联 字符计数