【问题标题】:integer is required(got type str)整数是必需的(得到类型 str)
【发布时间】:2020-02-27 15:22:04
【问题描述】:

这个程序计算两个日期之间的天数

这是错误:

line 14, in <module>
    f_date = date(d1, m1, y1)
ValueError: day is out of range for month
#program that counts the amount of days between two dates
from datetime import date

print("Hi,this program counts the amount of days between two dates, press enter and follow the instructions without writing spaces")

d1=int(input("tell me the day of the starting date"))
m1=int(input("now the month"))
y1=int(input("and the year"))

d2=int(input("now i would need the day of the the second date"))
m2=int(input("then the month"))
y2=int(input("and finally the year"))

f_date = date(d1, m1, y1)
l_date = date(d2, m2, y2)
delta = l_date - f_date
print(delta.days)


【问题讨论】:

  • 你输入了什么?
  • 是否有没有包含您提供的导致错误的输入的原因,以便可以重现它?

标签: python string date


【解决方案1】:

这是因为 datetime.date() 函数是 date(y, m ,d) 而不是 date(d, m ,y) https://docs.python.org/3/library/datetime.html#date-objects

此代码更改应该可以解决它(假设您的用户输入正确):

f_date = date(y1, m1, d1)
l_date = date(y2, m2, d2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多