您的位置:首页 > 其它

九宫格、函数入门、判断某天为某年的第几天

2018-01-28 23:57 323 查看

九宫格

# #九宫格:
# 1|2|3
# 4|5|6
# 7|8|9
# 横排相加=15,竖排相加=15,两对角相加等于15
num=[]
for i in range(1,10):
num.append(i)
#遍历x、y,当x!=y时,再遍历z,x!=z,y!=z,三个数就都不一样
L=[(x,y,z) for x in num for y in num if x!=y for z in num if x!=z and y!=z and x+y+z==15]

for L1 in L:
for L2 in L:
if set(L1) & set(L2):               #set集合,取出的第一排不能等于第二排
continue
for L3 in L:
if set(L1) & set(L2) & set(L3): #第一、二、三排都不等
continue
elif L1[0]+L2[0]+L3[0] != 15:   #竖排不等的话就跳过,横排肯定是相等的,所以不用判断
continue
elif L1[1]+L2[1]+L3[1] != 15:
continue
elif L1[1]+L2[1]+L3[1] != 15:
continue
elif L1[0]+L2[1]+L3[2] != 15:   #两对角不等的话就跳过
continue
elif L1[2]+L2[1]+L3[0] != 15:
continue
else:
print('''
{0}|{1}|{2}
{3}|{4}|{5}
{6}|{7}|{8}
'''.format(L1[0],L1[1],L1[2],L2[0],L2[1],L2[2],L3[0],L3[1],L3[2]))

函数入门



判断某天为某年的第几天:
思路:比如输入:2018-03-01,需要把2月份的天数加上,在加上当前月份的天数
判断是不是闰年:可以整除4,不可以整除100,但是可以整除400

dat = input('Enter a certain year and a certain day,format: yyyy-mm-dd :')
year=int(dat[0:4])
month=int(dat[5:7])
day=int(dat[8:])
Leap_year=1 #先定义不是闰年
if year % 4 == 0 and year % 100 != 0:
Leap_year=0   #判断是闰年
elif year%400 == 0:
Leap_year = 0    #判断是闰年
else:
Leap_year = 1

if Leap_year== 0:   #是闰年,2月份就是29天
ms = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
else:
ms = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

days=0
if month in range(1,13):
for i in range(month-1):
days+=ms[i]
print('{0} is this years {1} days'.format(dat,(days+day)))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 入门
相关文章推荐