使用算法python计算工作日

时间:2016-10-26 03:29:14

标签: python dayofweek leap-year

我正在编写一个程序,要求用户提供日期(日,月和年),并在一周中的某一天(星期一,星期二等)得到答案。根据他的算法: https://es.wikibooks.org/wiki/Algoritmia/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana 我收到了这个错误:

文件“C:/ Users / USUARIO / Documents /Programación/ Desafio 4 /WaldoMuñozdesafio4 / Dia de la semana55.py”,第64行,     Algoritmo =((年 - 1)%7 +((年 - 1)/ 4 - 3 *((年 - 1)/ 100 + 1)/ 4)%7 +月+日%7)%7

TypeError:+:'float'和'str'

的不支持的操作数类型

这是我到目前为止所做的:

day = int(input("Day of the month (number): "))
month = input("Name of the month: ")
month = month.lower()
year = int(input("The year is (numbers): "))

#In order to calculate the day of the week (Monday, ,Tuestday,etc)
#There are two cases: Leap year and non-leap.
if month == "january":
    month = 0
elif month == "february":
    month = 3
#These two months have equal module in leap year and non-leap.

elif month == "march":
    month = 3 #non-leap
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)): #condition to be leap
        month = 4
elif month == "april":
    month = 6
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 0
elif month == "may":
    month = 1
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 2
elif month == "june":
    month = 4
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 5
elif month == "july":
    month = 6
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 0
elif month == "august":
    month = 2
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 3
elif month == "september":
    month = 5
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 6
elif month == "october":
    month = 0
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 1
elif month == "november":
    month = 3
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 4
elif month == "december":
    month = 5
    if(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
        month = 6
else:
    print("Please, write the date with the correct format.")

Algoritmo = int((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7
#Algorithm to calculate day of the week

if Algoritmo == 0:
    print ("Monday")
elif Algoritmo == 1:
    print ("Tuesday")
elif Algoritmo == 2:
    print ("Wednesday")
elif Algoritmo == 3:
    print ("Thursday")
elif Algoritmo == 4:
    print ("Friday")
elif Algoritmo == 5:
    print ("Saturday")
elif Algoritmo == 6:
    print ("Sunday")

P.S。:我是一名西班牙语母语人士,如果有错误,我很抱歉......

1 个答案:

答案 0 :(得分:0)

错误最有可能发生在您错误拼写一个月,因为您没有输入错误或要求更正或以其他方式停止代码,它仍然是一个导致您看到错误的字符串

例如

>>> test()
Day of the month (number): 25
Name of the month: october
The year is (numbers): 2016
Tuesday
>>> test()
Day of the month (number): 25
Name of the month: octubre
The year is (numbers): 2016
Please, write the date with the correct format.
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    test()
  File "C:\Users\David\Documents\Python Scripts\stackoverflow_test.py", line 67, in test
    Algoritmo = int((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7
TypeError: unsupported operand type(s) for +: 'float' and 'str'
>>> 

你需要确保用户引入了正确的月份,为此你可以做这样的事情

months = {"january","february",...}#put all the month here
def ask_month():
    result=""
    while result not in months:
        result = input("Name of the month: ").lower()
    return result

你也重复这个太多了

(year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))

创建一个保存该值的新变量,然后使用它。

if-elif的长链也可以通过制作带有日期的列表并通过计算结果对其进行索引来缩减为几行,例如

days_names=["Monday", "Tuesday", ... ]
print( days_names[Algoritmo] )