本文共 1771 字,大约阅读时间需要 5 分钟。
# #九宫格:# 1|2|3# 4|5|6# 7|8|9# 横排相加=15,竖排相加=15,两对角相加等于15num=[]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,但是可以整除400dat = 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 = 1if 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=0if 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)))