Python学习-0410

一、练习题

(1)循环输入一串字符,统计其中字母、数字、空格,如要退出则输入‘exit’

 
while 1:
    str = input("input a  string (quit will be exit)")
    #统计数字
    number = []
    #统计空格
    space = 0
    #统计字母
    zm = []
    #其它字符
    qt = []
    #掉字符串头尾空格,并判断是否输入为‘exit’,如输入‘exit’则退出程序 
    if str.strip() == 'exit':
        print('Exit')
        exit(1)
    for i in str:
        if i.isdigit():
            number.append(i)
        elif i.isspace():
            space +=1
        elif i.isalpha():
            zm.append(i)
        else:
            qt.append(i)
    print("数字共出现了{0},它们分别是:{1}".format(len(number),"".join(number)))
    print("字母共出现了{0},它们分别是:{1}".format(len(zm),"".join(zm)))
    print("空格共出现了{0}".format(space))
    print("其它字符共出现了{0},它们分别是{1}".format(len(qt),"".join(qt)))
 

(2)ABCD乘9=DCBA,A=? B=? C=? D=? 答案:a=1,b=0,c=8,d=9      1089*9=9801

#已经确定 A必定等于1,为了提高程序效率直接输入1
for A in [1]:
    for B in range(0,10):
        for C in range(0,10):
            # 已经确定 D必定等于1,为了提高程序效率直接输入9
            for D in [9]:
                if ((1000 * A + 100 * B + 10 * C + D) * 9 == 1000 * D + 100 * C + 10 * B + A):
                    print("A = {0}".format(A))
                    print("B = {0}".format(B))
                    print("C = {0}".format(C))
                    print("D = {0}".format(D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A, B, C, D))

(3)九宫格 ,要求:在九宫格中输入1-9数字 横竖斜相加都为15

        格式如下:                       | A | B | C |
                                                | D | E | F |

                                                | G | H | I |

        思路:A的取值:1-9

                  B的取值:1-9 除去A

                  C的取值:1-9除去A,B  

                  除去的目的是为了提高程序效率


number = list()
for i in range(1, 10):
    number.append(i)
#用于统计可能出现的次数
count = 1
for A in number:
    #新建列表,用于除去列表中的"A",这里需要使用copy函数,不能使用“=”赋值,因为内存地址中还是指向的为一个地址
    a = number.copy()
    a.remove(A)
    for B in a:
        #新建列表用于除去列表中的“B”
        b = a.copy()
        b.remove(B)
        for C in b:
            c = b.copy()
            c.remove(C)
            for D in c:
                d = c.copy()
                d.remove(D)
                for E in d:
                    e = d.copy()
                    e.remove(E)
                    for F in e:
                        f = e.copy()
                        f.remove(F)
                        for G in f:
                            g = f.copy()
                            g.remove(G)
                            for H in g:
                                h = g.copy()
                                h.remove(H)
                                for I in h:
                                    #判断九宫格中能出现的横竖斜相加合等于15
                                    if (A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == G+E+C == 15):
                                        print('''
                                        {9}种例子
                                        | {0} | {1} | {2} |
                                        | {3} | {4} | {5} |
                                        | {6} | {7} | {8} |  '''.format(A,B,C,D,E,F,G,H,I,count))
                                        count += 1

(3)计算阶乘

        结果:0! +1! +2! + 3! +4! + 5! + n!

                  1 + 1 + 2 + 6 + …… + n*(n-1)*(n-2)……*1

#计算阶乘函数
def jc(n):
    result = 1
    #如果输入等于0则返回1
    if n == 0:
        return result
    else:
        for i in range(1, n+1):
            result *= i
        return result


n = input("plese inpurt number:")
count = 0
for i in range(0, int(n)+1):
    count += jc(i)

print("count = {0}".format(count))

二、Python的编码

        常用并支持中文的编码:utf-8、gbk 

        其它编码:gb2312

        如果编码不统一则会出现乱码情况

        python2中:

        中文存储编码为:ASCLL码,需要把编码改成为utf-8,python3中不存在这种情况

        python2中的转码过程:

        源有编码 -> unicode编码 -> 目的编码

        decode("utf-8")  解码  ->       unicode       ->  encode("gbk")  编码

        这是python2中的解码转码过程,在python3中则避免这一系列的解码转码过程

        解决乱码的方法:

        第一种代码中进行转码: 

       s.decode("uft-8").encode("gbk")

        第二种修改系统编码:

        

    import sys
    reload(sys)
    print(sys.getdefaultencoding())
    sys.setdefaultencoding(“utf-8”)
    print(sys.getdefaultencoding())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值