How can I check if a month has 31 days or not
Question:
Below is code to work out if a given date is invalid or not. How do I say:
(if 31 == days AND the month isn't 1,3,5,7,8,10 or 12) OR (there are 32 <= days
), date invalid.
I tried just using what I said above but I don’t think I understand how python OR statements work… the code looks long but is hopefully simple enough so that it doesn’t take to much time to read through.
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.")
Sorry if this took to long to read^
Answers:
I found some Python code to validate dates in an online tutorial (if that’s really your goal). Below is a copy of the code.
(Note that it does not use the calendar
module, 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")
Below is code to work out if a given date is invalid or not. How do I say:
(if 31 == days AND the month isn't 1,3,5,7,8,10 or 12) OR (there are 32 <= days
), date invalid.
I tried just using what I said above but I don’t think I understand how python OR statements work… the code looks long but is hopefully simple enough so that it doesn’t take to much time to read through.
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.")
Sorry if this took to long to read^
I found some Python code to validate dates in an online tutorial (if that’s really your goal). Below is a copy of the code.
(Note that it does not use the calendar
module, 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")