习题:九宫格
number = list()
for i in range(1,10):
number.append(i)
print(number)
count = 1
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:
if (A+B+C == D+E+F == G+H+I == 15) and (A+D+G == A+E+I == 15) and (B+E+H == 15) and (C+E+G == C+F+I == 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
总共有8个。
习题2:
A+B+C+D = D+C+B+A
for A in [1]: for B in range(0,10): for C in range(0,10): 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={4}{5}{6}{7}".format(A,B,C,D,D,C,B,A)) #或者: print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A,B,C,D))
Python3 的编码问题 改进的特别好。Python2 就有很多问题。
目前使用支持的编码格式有3个:utf-8 gbk gb-2312
python 2的默认编码格式就是ASCII,所以不支持中文
print(sys.getdefaultencoding())
需要识别中文的话 要在文件的头部增加# -*- coding: UTF-8 -*-
print(sys.getdefaultencoding())
print("文件编码")
使用Python2来运行这个段代码会出现乱码
如果指定格式就不会可以在字符串前加一个u来表示安装uft-8编码来存放。
print(sys.getdefaultencoding())
print(u"文件编码")
还可以使用解码方式来正确显示中文
a = "文件编码"
print(a.decode("utf-8"))
python 会自动的先将字符串解码为unicode ,然后在编码成gbk,因为解码是Python自动进行的,我们没有指明解码方式,Python 就会使用sys.defaultencoding指明的方式来解码。
还可以用这种方式:
s = "中文"
print(s.decode("utf-8").encode("gbk"))
先解码utf-8 在编码gbk,就可以显示了