【发布时间】:2016-07-07 05:34:00
【问题描述】:
我的部分中期项目是将日期(月和日)转换为数字天数(1-366,包括闰年),但我不确定如何执行此操作。我不知道如何使它工作,我也不知道如何使它,所以你只能输入有效月份(即错误,确保大写 1 月。)
以下是我到目前为止所做的。
#Months
January=0
February=31
March=60
April=91
May=121
June=152
July=182
August=213
September=244
October=274
November=305
December=335
#Title
print("Convert: Date to day and day to date.")
#user inputs a date
month=input("\nPick a Month (Make sure to capitalize the first letter): ")
day=int(input("Pick the day of the date you wish to input: "))
if day<0 or day>31:
print("Error, no months have greater than 31 days or less than 1 day.")
请帮忙。过去几天我一直在研究这个问题,但仍然不知道该怎么做。
【问题讨论】:
-
如果用户输入 12 月 31 日,您如何判断是第 365 天还是第 366 天?该程序是否只在闰年运行?
-
几个提示 - 使用字典而不是变量.. 月 = {'January': 0,.... }.. 输入并检查值是否与键匹配,如果因此,从字典中获取值并添加天数。这将为您提供一个简单的工作解决方案。现在考虑如何通过额外的验证来改进它
-
可以使用
datetime等标准库吗?如果是的话,我强烈建议你看看这个帖子stackoverflow.com/questions/620305/…
标签: python string python-3.x while-loop