一、九宫格

1、第一种方式

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/28 17:25
# @Author  : zhouyuyao
# @File    : nine.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32

'''
九宫格
___________
|_A_|_B_|_C_|
|_D_|_E_|_F_|
|_G_|_H_|_I _|
如果A取1到9,B则只能在1到9中取出A的值,C的值则从1到9中取出A和B,
每个数的取值都从1到9中取值,然后加入条件,这九个数之间存在的联系,
其一:9个数都不能相等,
其二:行和列每三个的数相加等于15

该算法比较慢,大概需要计算10的10次幂
'''
number = list()
for i in range(1,10):
    number.append(i)

for A in number:
    for B in number:
        for C in number:
            for D in number:
                for E in number:
                    for F in number:
                        for G in number:
                            for H in number:
                                for I in number:
                                    s = set()
                                    s.add(A)
                                    s.add(B)
                                    s.add(C)
                                    s.add(D)
                                    s.add(E)
                                    s.add(F)
                                    s.add(G)
                                    s.add(H)
                                    s.add(I)
                                    if (A+B+C) == (D+E+F) == (G+H+I) == (A+D+G) == (B+E+H) == (C+F+I) == (A+E+I) == (C+E+G) ==15 and len(s)==9:
                                        print("""
______________
|_{0}_|_{1}_|_{2}_|
|_{3}_|_{4}_|_{5}_|
|_{6}_|_{7}_|_{8}_|""".format(A,B,C,D,E,F,G,H,I))

这样的算法运行结果需要很长时间:

___________
|_2_|_7_|_6_|
|_9_|_5_|_1_|
|_4_|_3_|_8_|

___________
|_2_|_9_|_4_|
|_7_|_5_|_3_|
|_6_|_1_|_8_|

___________
|_4_|_3_|_8_|
|_9_|_5_|_1_|
|_2_|_7_|_6_|

___________
|_4_|_9_|_2_|
|_3_|_5_|_7_|
|_8_|_1_|_6_|

___________
|_6_|_1_|_8_|
|_7_|_5_|_3_|
|_2_|_9_|_4_|

___________
|_6_|_7_|_2_|
|_1_|_5_|_9_|
|_8_|_3_|_4_|

___________
|_8_|_1_|_6_|
|_3_|_5_|_7_|
|_4_|_9_|_2_|

___________
|_8_|_3_|_4_|
|_1_|_5_|_9_|
|_6_|_7_|_2_|

2、第二种方式

class NinePaper(object):
    def __init__(self):
        print('''
             ______________
            |____|____|____|
            |____|____|____|
            |____|____|____|
            A,B,C,E,F,G,H,I,J必须是1-9,所有行和列的三个数相加都等于15
        ''')
        self.numbers=list()
        for i in range(1,10):
            self.numbers.append(i)
        print("number = {0}".format(self.numbers))

    def run(self):
        for A in range(1,10):
            l1=list()
            l1+=self.numbers
            l1.remove(A)
            for B in l1:
                l2=list()
                l2+=l1
                l2.remove(B)
                for C in l2:
                    l3=list()
                    l3+=l2
                    l3.remove(C)
                    for D in l3:
                        l4=list()
                        l4+=l3
                        l4.remove(D)
                        for E in l4:
                            l5=list()
                            l5+=l4
                            l5.remove(E)
                            for F in l5:
                                l6=list()
                                l6+=l5
                                l6.remove(F)
                                for G in l6:
                                    l7=list()
                                    l7+=l6
                                    l7.remove(G)
                                    for H in l7:
                                        l8=list()
                                        l8+=l7
                                        l8.remove(H)
                                        for I in l8:
                                                if A+B+C==E+F+D==H+I+G==A+E+I==B+E+H==C+G+E==A+E+I==C+F+I==15:
                                                    print('''
 ___________
|_{0}_|_{1}_|_{2}_|
|_{3}_|_{4}_|_{5}_|
|_{6}_|_{7}_|_{8}_|
            '''.format(A,B,C,D,E,F,G,H,I))

        # ABC
        # DEF
        # GHI

def main():
    ninePaper=NinePaper()
    ninePaper.run()

if __name__ == '__main__':
    main()

二、函数入门

在我们需要解决一个问题时,我们可以把一个个问题分解成一个个模块,一个模块就是一个函数。 函数应是先申明,再调用,

def a(args):
    pass

函数代码块以def关键字开头,+函数名+参数 接着第一行内容应该缩进,然后是编写的代码逻辑 return关键字:代表函数执行完返回的值 pass关键字:代表什么都不干

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2018/1/28 21:56
# @Author  : zhouyuyao
# @File    : demon1.py
# PyCharm 2017.3.2 (Community Edition)
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) 
# [MSC v.1900 64 bit (AMD64)] on win32

def add(args):
    total = 0
    for i in args:
        total += i
    return total

def main():
    number = list()
    s = input("Please input digit like(a+b+c+d): ")
    for num in s.split("+"):     # 以+号为分隔符切割s字符串
        number.append(int(num.strip())) # num.strip() 去除前后的空格
    print(add(number))         # 最后得到的字符串 number 调用add函数

if __name__ == "__main__":     # 直接调用主函数
    main()