如何检查一个月是否有31天



以下是用于确定给定日期是否无效的代码。我该怎么说:

(if 31 == days AND the month isn't 1,3,5,7,8,10 or 12) OR (there are 32 <= days(,日期无效。

我试着只使用上面说的内容,但我不明白python OR语句是如何工作的。。。代码看起来很长,但希望足够简单,这样就不需要花太多时间来通读。

startMonthQuery = int(input(" What month are you starting in?n>"))
startDayQuery = int(input(" What day are you starting on?n>"))
if startMonthQuery <= 0:
print("That is not a valid month, please don't put zero or a negative number.")
elif startMonthQuery >= 13:
print("That is not a valid month, please do not put a thirteen or higher.")
if startDayQuery <= 0:
print("That is not a valid day, please do not put a zero or negatuve number.")
elif (startDayQuery >= 32)
print("That is not a valid day, it either doesn't have 31 days or you have entered a number far too high.")

很抱歉,如果阅读^花了很长时间

我在一个在线教程中找到了一些Python代码来验证日期(如果这真的是你的目标的话(。下面是代码的副本。

(请注意,使用calendar模块BTW。(

year = int(input("Enter year: "))
month = int(input("Enter month: "))
day = int(input("Enter day: "))
# Get Max value for a day in given month
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
max_day_value = 31
elif month == 4 or month == 6 or month == 9 or month == 11:
max_day_value = 30
elif year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
max_day_value = 29
else:
max_day_value = 28
if month < 1 or month > 12:
print("Date is invalid.")
elif day < 1 or day > max_day_value:
print("Date is invalid.")
else:
print("Valid Date")
import datetime
month=datetime.datetime.now()
if month.month in (1,3,5,7,8,10,12):
print("it has 31 days in this month")
elif month.month in(4,6,9,11):
print("it has 30 days ")
else:
print("it has 28 or 29 days")

最新更新