将用户指定的日期添加到现有代码中
我一直坚持能够将用户定义的日期添加到此 Days to Go 代码中。与嵌入的设定日期配合良好。但无法让它与输入线一起工作。
from datetime import datetime, time
b = input
event = (input('What is the name of your event?')) # input the name of the event
year = int(input('Enter a year')) # input the requires year
month = int(input('Enter a month')) # input the required month
day = int(input('Enter a day')) # input the required day
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime.date(b[1], b[2], b[3])
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
I'm a but stuck with being able to add a user defined date to this Days to Go code. Works well with a set date embedded. But can't get this to work with the input lines.
from datetime import datetime, time
b = input
event = (input('What is the name of your event?')) # input the name of the event
year = int(input('Enter a year')) # input the requires year
month = int(input('Enter a month')) # input the required month
day = int(input('Enter a day')) # input the required day
def date_diff_in_seconds(dt2, dt1):
timedelta = dt2 - dt1
return timedelta.days * 24 * 3600 + timedelta.seconds
def dhms_from_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
# Specified date
date1 = datetime.date(b[1], b[2], b[3])
# Current date
date2 = datetime.now()
print("\n%d days, %d hours, %d minutes, %d seconds" %
dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您错误地使用了
b=input
。这意味着您要使用函数名为 b 的input
函数,例如event = b('what is the name of your event?')
。相反,您可以在使用
input()
获取信息后将值分配给b
,例如b = (event,year,month,day)
。并且您通过
from datetime import datetime
导入了datetime
模块,您不需要明确地说datetime.date
,只需date.但是,您可以在此处使用
datetime
而不是date
,如下所示:结果如下:
First, you wrongly used
b=input
. It means you want to useinput
function with function name b, such asevent = b('what is the name of your event?')
.Instead, you can assign values to
b
likeb = (event, year, month, day)
after getting information usinginput()
.And you imported
datetime
module byfrom datetime import datetime
you don't need to explitly saydatetime.date
, justdate
. However, you can usedatetime
rather thandate
here, as follows:The result is like:
我认为你的问题可能是这一行:
尝试将其更改为:
I think your problem is likely this line:
Try changing it to this: