Program to check if a date is valid or not I am a novice programmer and am currently learning Python programming by myself through a book. I stumbled upon a question in a chapter and it took me 2 days to figure out a solution for it. Although, it may not be an efficient code for the problem, I would be grateful if you guys could review it. Just to let everyone know of my Knowledge level on programming, I just have basic knowledge of functions, sequences and decision structure.


  Write a program to check if a date is valid or not? The date should be
  in the mm/dd/yyyy format.
  
  Pseudocode:
  
  
  Define 3 functions to check the validity of month,day and year, if valid return True for each function.
  input a string date separated by a "/" and assign it to a date variable.
  split the date variable to 3 separate variables.
  check if the 3 variables are True or not.
  Print if the date is valid or not.
  


I didn't take into account leap years where February has 29 days and also assumed the year to start at 1 A.D. to current years.

def monthcheck(month):
        if month &gt; 0 and month &lt;= 12: ## If month is between 1 and 12, return True.
            return True
        else:
            return False



def daycheck(month,day):
    monthlist1 = [1,3,5,7,8,10,12] ## monthlist for months with 31 days.
    monthlist2 = [4,6,9,11] ## monthlist for months with 30 days.
    monthlist3 = 2 ## month with month with 28 days.

    for mon in monthlist1: ## iterate through monthlist1.
        if month == mon: ## Check if the parameter month equals to any month with 31 days.
            if day &gt;=1 and day &lt;= 31: ## If the parameter day is between 1 and 31, return True.
                return True
            else:
                return False

    for mon in monthlist2: ## iterate through the monthlist with 30 days.
        if month == mon: ## check if the parameter month equals to any month with 30 days.
            if day &gt;= 1 and day &lt;= 30: ## if the parameter day is between 1 and 30,return True.
                return True
            else:
                return False

    if month == monthlist3: ## check if parameter month equals month with 28 days.
        if day &gt;=1 and day &lt;= 28: ## if the parameter day is between 1 and 28,return True.
            return True
        else:
            return False



def yearcheck(year):
    if len(year) &gt;= 1 and len(year) &lt;= 4: ## Check if year has between 1 to 4 numbers and return True.
        return True
    else:
        return False


def main():
    date = str(input("Enter the date in mm/dd/yyyy format: ")) ## Input date in the given format.

    month,day,year = date.split("/") ## split the date into 3 separate variables.

    monthvalidity = monthcheck(int(month)) 

    dayvalidity = daycheck(int(month),int(day)) 

    yearvalidity = yearcheck(year) 

    if monthvalidity == True and dayvalidity == True and yearvalidity == True: ## check if all 3 variables are valid or True
        print("The date {0} is valid.".format(date))

    else:
        print("The date {0} is invalid.".format(date))

main()
Good work! While there is certainly room for improvement, it is good that you stuck it out and discovered a solution on your own.


def monthcheck(month):
    if month &gt; 0 and month &lt;= 12: ## If month is between 1 and 12, return True.
        return True
    else:
        return False


Can be more succinctly written as

def monthcheck(month):
    return 0 &lt; month &lt;= 12

While writing comments for practically every line of code can help you learn, and is a good way to keep track of where you are when working through pseudocode, don't just write what the code already told you.
PEP 8 - the style guide for Python - recommends using snake_case for naming functions, so monthcheck would become month_check.
What happens if I input "test" when asked for a date? The program crashes. It's a good idea to validate that any input from the user is in the format you expect.
The program asks for input in the format mm/dd/yyyy but will accept m/d/y, which probably isn't desired. 
Don't compare explicitly to True / False

if monthvalidity == True and dayvalidity == True and yearvalidity == True:


Can be more easily read if it is written as

if monthvalidity and dayvalidity and yearvalidity

daycheck has several locations in which it could be improved. You could use a dictionary (as heather does), but if you haven't learned about those yet, you can still improve the algorithm. 


Python's in operator can also check if a value is in a list, so instead of looping over monthlist1 and monthlist2, you can just check if month in monthlist1. 
Since monthlist1 doesn't really describe what is in the variable, and I am having difficulty coming up with a better name, I would just inline the list with if month in [1, 3, 5, 7, 8, 10, 12] and add a clarifying comment.

Don't be afraid to use natural sounding variable names. I believe monthcheck is better named is_month_valid.
What about negative numbers? 1/1/-111 is apparently a valid date. This might be desired, but probably deserves a comment.
Converting numbers to strings to determine the size is generally not a good idea, and can get you into trouble in more complex cases with floats / doubles. I would prefer the check 1 &lt;= year &lt; 10000.
There is no need to have str() around an input function, input returns a string already.




Python, as a "batteries included" language, has a feature that can do this for us! In production code, this is what you should use.

from datetime import datetime

def main():
    user_input = input('Enter the date in mm/dd/yyyy format: ')

    try:
        datetime.strptime(user_input, '%m/%d/%Y')
        print('The date {} is valid.'.format(user_input))
    except ValueError:
        print('The date {} is invalid'.format(user_input))


if __name__ == '__main__':
    main()


This was rather long, but don't worry! It is completely normal to get a lot of feedback when you are starting out. The more you code, the more natural it becomes.