python - 整数是必需的(得到类型 str)
问题描述
这个程序计算两个日期之间的天数
这是错误:
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)
解决方案
这是因为 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)
推荐阅读
- node.js - 现有的 nodeJS 和 swagger JS 文档不起作用
- java - Java - 二进制插入排序 StackOverFlowError
- python - 在系列 1..系列 1 中查找系列 2 中元素的位置:[1,2,3,4,5,6,7] 系列 2[1,3,7] 示例输出:[0,2,6]
- .net-core - 如何修复错误:“Google.Apis.Auth:应为 YouTubeAPI 设置至少一个客户端机密(已安装或 Web)”
- laravel - 检查日期时间是否在数据库 Laravel 6 中的两个日期时间之间
- javascript - 如何使用来自后端的数据和 node.js 在前端绘制图表?
- amazon-web-services - 解决 AWS CDK CloudFormation 堆栈之间的循环依赖关系
- python - 为一组股票数据python寻找趋势线
- c++ - 如何使用对 C 函数和类对象的外部调用来处理 C++ 头文件
- java - 为什么我必须在分配时强制转换