如何验证生日是否通过?

时间:2012-10-25 02:52:03

标签: python date if-statement case

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

如果乔小于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.")

2 个答案:

答案 0 :(得分:5)

我认为这是一项作业,无论如何都要查看datetime模块,这对于生成和比较日期非常方便。

答案 1 :(得分:0)

这看起来像是一个作业问题,您还应该查看dateutil.parser,它会自动以不同的格式自然地转换日期,因此您无需亲自解析它们。