# 九宫格--函数入门

2018-02-27 11:23:58来源:oschina作者:c1awn人点击

1. 九宫格
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
# 1-9九个数字放进九宫格，横竖斜求和都是15
lines = list()
for i in xrange(1,10):
for j in xrange(1,10):
if i == j:
continue
for k in xrange(1,10):
if k == i:
continue
if k == j:
continue
lines.append( (i,j,k) )
print(lines)
count = 0
for line1 in lines:
for line2 in lines:
if line2[0] in line1: #如果第二行第一个在第一行，跳出
continue
if line2[1] in line1:#如果第二行第二个在第一行，跳出
continue#不用判断第三个，因为和是15
for line3 in lines: #取第三行
if line3[0] in line1:
continue
if line3[1] in line1:
continue
if line3[0] in line2:
continue
if line3[1] in line2:
continue
if line1[0] + line1[1] + line1[2] != 15:
continue
if line2[0] + line2[1] + line2[2] != 15:
continue
if line3[0] + line3[1] + line3[2] != 15:
continue
if line1[0] + line2[0] + line3[0] != 15:
continue
if line1[1] + line2[1] + line3[1] != 15:
continue
if line1[2] + line2[2] + line3[2] != 15:
continue
if line1[0] + line2[1] + line3[2] != 15:
continue
if line1[2] + line2[1] + line3[0] != 15:
continue
count += 1
print("num:{0}/n{1}/n{2}/n{3}/n".format(count,line1,line2,line3))

C:/Users/chawn/PycharmProjects/pyex/venv/Scripts/python.exe C:/Users/chawn/PycharmProjects/pyex/17年12月/171226/九宫格.py
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 4, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 2), (1, 5, 3), (1, 5, 4), (1, 5, 6), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 2), (1, 6, 3), (1, 6, 4), (1, 6, 5), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7, 2), (1, 7, 3), (1, 7, 4), (1, 7, 5), (1, 7, 6), (1, 7, 8), (1, 7, 9), (1, 8, 2), (1, 8, 3), (1, 8, 4), (1, 8, 5), (1, 8, 6), (1, 8, 7), (1, 8, 9), (1, 9, 2), (1, 9, 3), (1, 9, 4), (1, 9, 5), (1, 9, 6), (1, 9, 7), (1, 9, 8), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 1, 6), (2, 1, 7), (2, 1, 8), (2, 1, 9), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 1), (2, 5, 3), (2, 5, 4), (2, 5, 6), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 1), (2, 6, 3), (2, 6, 4), (2, 6, 5), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7, 1), (2, 7, 3), (2, 7, 4), (2, 7, 5), (2, 7, 6), (2, 7, 8), (2, 7, 9), (2, 8, 1), (2, 8, 3), (2, 8, 4), (2, 8, 5), (2, 8, 6), (2, 8, 7), (2, 8, 9), (2, 9, 1), (2, 9, 3), (2, 9, 4), (2, 9, 5), (2, 9, 6), (2, 9, 7), (2, 9, 8), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 1, 6), (3, 1, 7), (3, 1, 8), (3, 1, 9), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 2, 6), (3, 2, 7), (3, 2, 8), (3, 2, 9), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 1), (3, 5, 2), (3, 5, 4), (3, 5, 6), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 1), (3, 6, 2), (3, 6, 4), (3, 6, 5), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7, 1), (3, 7, 2), (3, 7, 4), (3, 7, 5), (3, 7, 6), (3, 7, 8), (3, 7, 9), (3, 8, 1), (3, 8, 2), (3, 8, 4), (3, 8, 5), (3, 8, 6), (3, 8, 7), (3, 8, 9), (3, 9, 1), (3, 9, 2), (3, 9, 4), (3, 9, 5), (3, 9, 6), (3, 9, 7), (3, 9, 8), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 1, 6), (4, 1, 7), (4, 1, 8), (4, 1, 9), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 2, 6), (4, 2, 7), (4, 2, 8), (4, 2, 9), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 3, 6), (4, 3, 7), (4, 3, 8), (4, 3, 9), (4, 5, 1), (4, 5, 2), (4, 5, 3), (4, 5, 6), (4, 5, 7), (4, 5, 8), (4, 5, 9), (4, 6, 1), (4, 6, 2), (4, 6, 3), (4, 6, 5), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 7, 1), (4, 7, 2), (4, 7, 3), (4, 7, 5), (4, 7, 6), (4, 7, 8), (4, 7, 9), (4, 8, 1), (4, 8, 2), (4, 8, 3), (4, 8, 5), (4, 8, 6), (4, 8, 7), (4, 8, 9), (4, 9, 1), (4, 9, 2), (4, 9, 3), (4, 9, 5), (4, 9, 6), (4, 9, 7), (4, 9, 8), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 1, 6), (5, 1, 7), (5, 1, 8), (5, 1, 9), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 2, 6), (5, 2, 7), (5, 2, 8), (5, 2, 9), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 3, 6), (5, 3, 7), (5, 3, 8), (5, 3, 9), (5, 4, 1), (5, 4, 2), (5, 4, 3), (5, 4, 6), (5, 4, 7), (5, 4, 8), (5, 4, 9), (5, 6, 1), (5, 6, 2), (5, 6, 3), (5, 6, 4), (5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 7, 1), (5, 7, 2), (5, 7, 3), (5, 7, 4), (5, 7, 6), (5, 7, 8), (5, 7, 9), (5, 8, 1), (5, 8, 2), (5, 8, 3), (5, 8, 4), (5, 8, 6), (5, 8, 7), (5, 8, 9), (5, 9, 1), (5, 9, 2), (5, 9, 3), (5, 9, 4), (5, 9, 6), (5, 9, 7), (5, 9, 8), (6, 1, 2), (6, 1, 3), (6, 1, 4), (6, 1, 5), (6, 1, 7), (6, 1, 8), (6, 1, 9), (6, 2, 1), (6, 2, 3), (6, 2, 4), (6, 2, 5), (6, 2, 7), (6, 2, 8), (6, 2, 9), (6, 3, 1), (6, 3, 2), (6, 3, 4), (6, 3, 5), (6, 3, 7), (6, 3, 8), (6, 3, 9), (6, 4, 1), (6, 4, 2), (6, 4, 3), (6, 4, 5), (6, 4, 7), (6, 4, 8), (6, 4, 9), (6, 5, 1), (6, 5, 2), (6, 5, 3), (6, 5, 4), (6, 5, 7), (6, 5, 8), (6, 5, 9), (6, 7, 1), (6, 7, 2), (6, 7, 3), (6, 7, 4), (6, 7, 5), (6, 7, 8), (6, 7, 9), (6, 8, 1), (6, 8, 2), (6, 8, 3), (6, 8, 4), (6, 8, 5), (6, 8, 7), (6, 8, 9), (6, 9, 1), (6, 9, 2), (6, 9, 3), (6, 9, 4), (6, 9, 5), (6, 9, 7), (6, 9, 8), (7, 1, 2), (7, 1, 3), (7, 1, 4), (7, 1, 5), (7, 1, 6), (7, 1, 8), (7, 1, 9), (7, 2, 1), (7, 2, 3), (7, 2, 4), (7, 2, 5), (7, 2, 6), (7, 2, 8), (7, 2, 9), (7, 3, 1), (7, 3, 2), (7, 3, 4), (7, 3, 5), (7, 3, 6), (7, 3, 8), (7, 3, 9), (7, 4, 1), (7, 4, 2), (7, 4, 3), (7, 4, 5), (7, 4, 6), (7, 4, 8), (7, 4, 9), (7, 5, 1), (7, 5, 2), (7, 5, 3), (7, 5, 4), (7, 5, 6), (7, 5, 8), (7, 5, 9), (7, 6, 1), (7, 6, 2), (7, 6, 3), (7, 6, 4), (7, 6, 5), (7, 6, 8), (7, 6, 9), (7, 8, 1), (7, 8, 2), (7, 8, 3), (7, 8, 4), (7, 8, 5), (7, 8, 6), (7, 8, 9), (7, 9, 1), (7, 9, 2), (7, 9, 3), (7, 9, 4), (7, 9, 5), (7, 9, 6), (7, 9, 8), (8, 1, 2), (8, 1, 3), (8, 1, 4), (8, 1, 5), (8, 1, 6), (8, 1, 7), (8, 1, 9), (8, 2, 1), (8, 2, 3), (8, 2, 4), (8, 2, 5), (8, 2, 6), (8, 2, 7), (8, 2, 9), (8, 3, 1), (8, 3, 2), (8, 3, 4), (8, 3, 5), (8, 3, 6), (8, 3, 7), (8, 3, 9), (8, 4, 1), (8, 4, 2), (8, 4, 3), (8, 4, 5), (8, 4, 6), (8, 4, 7), (8, 4, 9), (8, 5, 1), (8, 5, 2), (8, 5, 3), (8, 5, 4), (8, 5, 6), (8, 5, 7), (8, 5, 9), (8, 6, 1), (8, 6, 2), (8, 6, 3), (8, 6, 4), (8, 6, 5), (8, 6, 7), (8, 6, 9), (8, 7, 1), (8, 7, 2), (8, 7, 3), (8, 7, 4), (8, 7, 5), (8, 7, 6), (8, 7, 9), (8, 9, 1), (8, 9, 2), (8, 9, 3), (8, 9, 4), (8, 9, 5), (8, 9, 6), (8, 9, 7), (9, 1, 2), (9, 1, 3), (9, 1, 4), (9, 1, 5), (9, 1, 6), (9, 1, 7), (9, 1, 8), (9, 2, 1), (9, 2, 3), (9, 2, 4), (9, 2, 5), (9, 2, 6), (9, 2, 7), (9, 2, 8), (9, 3, 1), (9, 3, 2), (9, 3, 4), (9, 3, 5), (9, 3, 6), (9, 3, 7), (9, 3, 8), (9, 4, 1), (9, 4, 2), (9, 4, 3), (9, 4, 5), (9, 4, 6), (9, 4, 7), (9, 4, 8), (9, 5, 1), (9, 5, 2), (9, 5, 3), (9, 5, 4), (9, 5, 6), (9, 5, 7), (9, 5, 8), (9, 6, 1), (9, 6, 2), (9, 6, 3), (9, 6, 4), (9, 6, 5), (9, 6, 7), (9, 6, 8), (9, 7, 1), (9, 7, 2), (9, 7, 3), (9, 7, 4), (9, 7, 5), (9, 7, 6), (9, 7, 8), (9, 8, 1), (9, 8, 2), (9, 8, 3), (9, 8, 4), (9, 8, 5), (9, 8, 6), (9, 8, 7)]
num:1
(2, 7, 6)
(9, 5, 1)
(4, 3, 8)
num:2
(2, 9, 4)
(7, 5, 3)
(6, 1, 8)
num:3
(4, 3, 8)
(9, 5, 1)
(2, 7, 6)
num:4
(4, 9, 2)
(3, 5, 7)
(8, 1, 6)
num:5
(6, 1, 8)
(7, 5, 3)
(2, 9, 4)
num:6
(6, 7, 2)
(1, 5, 9)
(8, 3, 4)
num:7
(8, 1, 6)
(3, 5, 7)
(4, 9, 2)
num:8
(8, 3, 4)
(1, 5, 9)
(6, 7, 2)Process finished with exit code 0
2. 函数
1. 函数定义

return [表达式] 结束函数，选择性地返回一个值给调用方。不带表达式的return相当于返回 None。

def定义函数

def fun():
print "hello"调用函数,直接调用之前定义的
In [3]: fun()
hello再举一个复杂一点的例子，定义sth函数判断是否数字：
In [4]: def fun():
...: sth = raw_input("input a num: ")
...: try:
...:if type(int(sth)) == type(1):
...:print "%s is a num " %sth
...: except:
...:print "%s is not a num" %sth

try和excpet用来判断

In [5]: fun()
input a num: 1a
1a is not a num
In [6]: fun()
input a num: 5
5 is a num
2. 函数参数

In [14]: def fun(x,y):
....: if x >= y:
....:print "%s is bigger" %x
....: else:
....:print "%s is bigger" %y
....:

In [15]: fun(3,3)
3 is bigger
In [16]: fun(3,4)
4 is bigger
In [17]: fun(3,2)
3 is bigger举例2：
#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
import sys
def isNum(n):#定义函数最好类似这么写
for i in n:
if i in "0123456789":
pass#占位
else:
print "%s is not a num" %n
sys.exit()#跳出整个程序
else:
print "%s is a num" %n
isNum(sys.argv[1])

sys.argv[1]输出第一个参数，[0]是程序名本身，[1]是第一个参数，以此类推

[[email protected] py]# python 1.py 5
5 is a num
[[email protected] py]# python 1.py 5a
5a is not a num
3. 判断某天为第几天
#!/usr/bin/env python
# -*- coding:utf8 -*-
# @TIME: 2018/2/26 21:33
# @AUTHOR: Chawn
# FILE: 判断某天是第几天.py
year=int(input('请输入年:'))
month=int(input('请输入月:'))
day=int(input('请输入天:'))
sum=day
days = [31,28,31,30,31,30,31,31,30,31,30,31]
i=0
if ( year%4 == 0 and year%100 != 0) or (year%400 == 0):
days[1] = 29
while i< month-1:
sum=sum+days[i]
i+=1
print '这一天是该年的第',sum,'天'
C:/Users/chawn/PycharmProjects/pyex/venv/Scripts/python.exe C:/Users/chawn/PycharmProjects/pyex/判断某天是第几天.py

Process finished with exit code 0