将用户指定的日期添加到现有代码中

发布于 2025-01-15 06:01:08 字数 983 浏览 3 评论 0原文

我一直坚持能够将用户定义的日期添加到此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2025-01-22 06:01:08

首先,您错误地使用了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,如下所示:

from datetime import datetime, time

#b = input -> wrong usage
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
b = (event, year, month, day) # you can assign date values to b

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(b[1], b[2], b[3]) # not datetime.date()

# 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()

# if you want to print the event together:
print("\n%d days, %d hours, %d minutes, %d seconds left for %s" % (
            dhms_from_seconds(date_diff_in_seconds(date2, date1)) + (event,)))

结果如下:

What is the name of your event? birthday
Enter a year 2022
Enter a month 03
Enter a day 19

0 days, 14 hours, 40 minutes, 2 seconds
0 days, 14 hours, 40 minutes, 2 seconds left for Sunday # in case that you print the event together

First, you wrongly used b=input. It means you want to use input function with function name b, such as event = b('what is the name of your event?').

Instead, you can assign values to b like b = (event, year, month, day) after getting information using input().

And you imported datetime module by from datetime import datetime you don't need to explitly say datetime.date, just date. However, you can use datetime rather than date here, as follows:

from datetime import datetime, time

#b = input -> wrong usage
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
b = (event, year, month, day) # you can assign date values to b

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(b[1], b[2], b[3]) # not datetime.date()

# 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()

# if you want to print the event together:
print("\n%d days, %d hours, %d minutes, %d seconds left for %s" % (
            dhms_from_seconds(date_diff_in_seconds(date2, date1)) + (event,)))

The result is like:

What is the name of your event? birthday
Enter a year 2022
Enter a month 03
Enter a day 19

0 days, 14 hours, 40 minutes, 2 seconds
0 days, 14 hours, 40 minutes, 2 seconds left for Sunday # in case that you print the event together
太阳公公是暖光 2025-01-22 06:01:08

我认为你的问题可能是这一行:

date1 = datetime.date(b[1],b[2],b[3])

尝试将其更改为:

date1 = datetime.date(year, month, day, 0, 0, 0)

I think your problem is likely this line:

date1 = datetime.date(b[1],b[2],b[3])

Try changing it to this:

date1 = datetime.date(year, month, day, 0, 0, 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文