【问题标题】:Calculate weekdays with algorithm python用算法python计算工作日
【发布时间】:2017-03-08 06:51:06
【问题描述】:

我正在编写一个程序,它会询问用户一个日期(日、月和年),然后你会得到一周中的哪一天(星期一、星期二等)作为答案。根据他的算法: 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 行,在 算法 = ((年 - 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.:我是西班牙语母语者,如有错误请见谅...

【问题讨论】:

  • 你有什么问题?
  • 对不起...我得到这个:文件“C:/Users/USUARIO/Documents/Programación/Desafio 4/Waldo Muñoz desafio 4/Dia de la semana55.py”,第 64 行,在 算法 = ((year - 1) % 7 + ((year - 1) / 4 - 3 * ((year - 1) / 100 + 1) / 4) % 7 + month + day % 7) % 7 类型错误:+ 的不支持的操作数类型:“float”和“str”
  • 你少了一个引号@if month == january":
  • 已更正! (翻译成英文时出错)......我仍然有同样的错误
  • 收到此错误时您的输入是什么?

标签: python dayofweek leap-year


【解决方案1】:

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

例如

>>> 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] )

【讨论】:

  • 感谢您的提示!
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
相关资源
最近更新 更多