使用算法 python 计算工作日
Calculate weekdays with algorithm python
我正在编写一个程序,询问用户日期(日、月和年),然后您得到星期几(星期一、星期二等)的回答。根据他的算法:
https://es.wikibooks.org/wiki/Algoritmia/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana
我收到此错误:
文件 "C:/Users/USUARIO/Documents/Programación/Desafio 4/Waldo Muñoz desafio 4/Dia de la semana55.py",第 64 行,在
Algoritmo = ((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 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.: 本人母语是西班牙语,如有错误请见谅...
当您拼错月份时最有可能出现错误,因为您没有抛出错误或要求更正或以其他方式停止代码,它仍然是一个字符串,导致您看到的错误
例如
>>> 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\Whosebug_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] )
我正在编写一个程序,询问用户日期(日、月和年),然后您得到星期几(星期一、星期二等)的回答。根据他的算法: https://es.wikibooks.org/wiki/Algoritmia/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana 我收到此错误:
文件 "C:/Users/USUARIO/Documents/Programación/Desafio 4/Waldo Muñoz desafio 4/Dia de la semana55.py",第 64 行,在 Algoritmo = ((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 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.: 本人母语是西班牙语,如有错误请见谅...
当您拼错月份时最有可能出现错误,因为您没有抛出错误或要求更正或以其他方式停止代码,它仍然是一个字符串,导致您看到的错误
例如
>>> 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\Whosebug_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] )