前段时间一直跟着D大开始学习python,新接触语言,感觉挺简单的,写一个一简单的计算泽勒一致性,程序但是怎么运行都不对,请各位帮看看,实在搞不清楚问题在哪里。
- # 找星期程序,使用泽勒算法,
- #公式 h= (q + [26*(m+1)/10]+k+[k/4]+[j/4]+5j)%7#
- #已知bug 输入年月日不能输入为小数,报错。
- month = 0 #"""3-12月份为3-12,1,2 月份使用前一年的13,14月"""
- cent = 0 #'''世纪数'''
- year = 0 #'''世纪内年份'''
- Q = 0 #'''哪一天'''
- H = 0
-
- #年月日标志位用于判断输入数据合法性
- yearFlug = 1
- monthFlug = 1
- dayFlug = 1
-
-
- #接收用户输入年月日,并判断是否合法
- while(yearFlug):
- yearTemp = int(input("Please Enter Year :(eg:2008):"))
- if yearTemp > 0 :
- yearFlug = 0
- else:
- print("Sorry, Enter Wrong Year,try again Please")
- cent = yearTemp //100
- year = yearTemp % 100
- while(monthFlug):
- monthTemp = int(input("Please Enter month:(eg:10):"))
- if monthTemp > 0 and monthTemp <= 12:
- monthFlug = 0
- else:
- print("Sorry, Enter Wrong month ,Try again Please")
- if monthTemp ==1 or monthTemp ==2:
- year -= 1
- month = monthTemp + 12
- while(dayFlug):
- dayTemp = int(input("Please Enter day:(eg:21):"))
- if dayTemp > 0 and dayTemp <= 28:
- dayFlug = 0
- #如果为大月 31天
- elif (monthTemp == 1 or monthTemp == 3 or monthTemp == 5 \
- or monthTemp == 7 or monthTemp == 8 or monthTemp == 10 \
- or monthTemp == 12) \
- and (dayTemp >28 and dayTemp <= 31):
- dayFlug = 0
- #如果为小月 30天
- elif (monthTemp == 4 or monthTemp == 6 or monthTemp == 9 or monthTemp == 11)\
- and (dayTemp >28 and dayTemp <= 30):
- dayFlug = 0
- #如果如果闰年2月为29天
- elif (monthTemp == 2) and (yearTemp %4 ==0 and yearTemp%100 != 0) and(dayTemp == 29):
- dayFlug = 0
- else:
- print("Sorry ,Enter Wrong day,Try again Please")
- Q = dayTemp
- #H = (Q + int(2.6*( month +1 )) \
- # + int(0.25 * year) +year \
- # + int(0.25 * cent) +5 *cent)\
- # % 7
- H = (Q + ((26 * ( month + 1 ) / 10 ) // 1 ) \
- + (( year / 4 ) // 1) + year \
- + (( cent / 4 ) // 1) \
- + ( 5 * cent) )\
- % 7
- #H =int(( Q +26*(month+1)/10+year+year/4+cent/4+5*cent)%7)
- if H == 0:
- weekDay = "Sat"
- elif H == 1:
- weekDay = "Sun"
- elif H == 2:
- weekDay = "Mon"
- elif H == 3:
- weekDay = "Tue"
- elif H == 4:
- weekDay = "Wed"
- elif H == 5:
- weekDay = "Thu"
- elif H == 6:
- weekDay = "Fri"
-
-
- print("The day you want check is ",yearTemp,".",monthTemp,".",dayTemp)
- print("Day of week is ",weekDay)
计算结果一直不正确。
比如计算今天怎么算都是周一
重新改一下发现好了!程序自己写错了……改了几句就好了!已经解决,小坑一个!
前面一直以为是自己算法公式错了,改来改去,刚才才发现,month没有值!
- if monthTemp ==1 or monthTemp ==2:
- year -= 1
- month = monthTemp + 12
- else:month = monthTemp
本帖最后由 strong161 于 2016-9-23 11:02 编辑