python学习-第5课

一、练习题

1.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

while 1:
    s1 = input("please input info: ")
    strs, ints, space, other = 0, 0, 0, 0
    if s1 == "quit":
        exit(1)
    for i in s1:
        if i.isalpha():
            strs += 1
        elif i.isdigit():
            ints += 1
        elif i.isspace():
            space += 1
        else:
            other += 1

    print("英文字母有{0},数字有{1},空格有{2},其它字符有{3}".format(strs,ints,space,other))

2.乘法口诀
for i in range (1,10):

    for j in range(1,i+1):
        print("{0}*{1}={2}".format(j,i,j*i),end=" ")

    print("")


3.九宫格
number = [1,2,3,4,5,6,7,8,9]
num=0
for A in number:
    a=number.copy()
    a.remove(A)
    for B in a:
        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:
                                    i=h.copy()
                                    i.remove(I)
                                    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:
                                        num+=1
                                        print('''
                                           第{9}种排序
                                           -------
                                           |{0}|{1}|{2}|
                                           |{3}|{4}|{5}|
                                           |{6}|{7}|{8}|
                                           -------
                                           '''.format(A, B, C, D, E, F, G, H, I,num))


4.ABCD乘9=DCBA,A=? B=? C=? D=? 答案:a=1,b=0,c=8,d=9      1089*9=9801
for A in [1]:
    for B in range(0, 10):
        for C in range(0, 10):
            for D in [9]:
                if ((A * 1000 + B * 100 + C * 10 + D) * 9 == D * 1000 + C * 100 + B * 10 + A):
                    print("A值为:{0}, B值为:{1}, C值为:{2}, D值为:{3}".format(A, B, C, D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A, B, C, D))


5.求0! + 1! + 2! + 3! + 4! + 5! + 6!+ n!和
def jc(n):
    result=1
    if n==0:
        return result
    else:
        for i in range(1,n+1):
            result *= int(i)
        return result


while 1:
    nums=input("please input a number: ")
    sums=0
    if nums=="e":
        exit(1)
    for i in range(0,int(nums)+1):
        sums+=jc(i)
    print("{0}阶乘为{1}".format(nums,sums))



二、编码转换报错(python2)

编码
支持中文的编码:
utf-8, gbk, gb2312
decode      解码
encode            编码

编码格式为utf-8
#-*-coding:utf-8 -*-
s="我们在学习python"
print(s)
在utf-8格式环境中能正常运行
我们在学习python
在gbk格式环境中运行乱码(如windows DOS环境)
鎴戜滑鍦ㄥ涔爌ython

方法一:
第一种方法
修改编码格式
#-*-coding:gbk -*-
s="我们在学习python"
print(s)

方法二:

文件申明是utf-8的编码,以unicode对象的形式存在。如果我们用type查看,存储形式是unicode,python在向控制台输出unicode对象的时候会自动根据输出环境的编码进行转换。如果输出的不是unicode对象而是str类型。则会按照字符串的编码输出字符串。从而出现utf8没法在gbk编码的控制台展现
原有编码   ->    unicode编码    -> 目的编码
decode("utf-8")  解码  -> unicode ->  encode("gbk")  编码
s1="我们在学习python"
s1.decode(“utf-8”).encode(“gbk”)

方法三:

import sys
reload(sys)   #修改系统编码格式
print(sys.getdefaultencoding())
sys.setdefaultencoding("utf-8")













猜你喜欢

转载自blog.csdn.net/biankm_gz/article/details/79899326