【问题标题】:How do I verify if a birthday is passed or not?如何验证生日是否已过?
【发布时间】:2012-10-25 02:52:03
【问题描述】:

这与我的上一个问题相似。但是这里我想知道如何知道一个人是否可以做某事,例如:

如果 joe 不到 21 岁,他就不能喝酒,用生日来告诉你他能不能喝。不使用导入或日历,试图掌握它的窍门

到目前为止,这就是我想出的。

dateStr = input("Today's Date: ")
monthStr, dayStr, yearStr = dateStr.split("/")
months = ["January", "February","March", "April", "May", "June", "July", "August","September","October", "November","December"]
monthStr = months[int(monthStr)- 1]
print ("The converted date is:", monthStr, dayStr+",", yearStr)
print("Joe did NOT have his birthday this year!")
Joe's birthday = True
for chr in dateStr:
  tv = True
  if:
    (i<= 13)
    print("Joe is Not allowed to watch TV.")
  else:
    print("Joe is allowed to watch TV.")
driving = True
if:
  (i<= 16)
    print("Joe is NOT Allowed to drive")
else:
  print("Joe is allowed to drive")
  drinking = True 
if:
  (i<= 21)
    print("Joe is NOT allowed to drink.")
else:
  print("Joes is allowed to drink.")

【问题讨论】:

  • 可以将生日和现在转换为时间戳,取其差,并将其与 X 年的秒数进行比较。

标签: python date if-statement case


【解决方案1】:

我假设这是一个分配,无论如何检查datetime模块,它非常方便生成和比较日期。

【讨论】:

    【解决方案2】:

    这似乎是一个作业问题,您还应该查看dateutil.parser,它可以自动将日期转换为不同格式,因此您不必自己解析它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多