I have written code to check any birthday input's validity. As I am new in programming, and after going through several debugging steps, the code became very ugly.
month_dict = {'jan':'January',
'feb':'February',
'mar':'March',
'may':'May',
'jul':'July',
'sep':'September',
'oct':'October',
'dec':'December',
'apr':'April',
'jun':'June',
'aug':'August',
'nov':'November'}
day = int(raw_input ('Enter your birth day: '))
month = raw_input ("Enter your birth month: ")
year_input = int (raw_input ('Enter your birth year: '))
days_31 = ['jan', 'mar', 'may', 'jul', 'aug', 'oct', 'dec']
days_30 = ['apr', 'jun', 'sep', 'nov']
days_28 = ['feb']
def valid_day_finding ():
global valid_day
if month_name in days_31:
if day > 0 and day < 32:
valid_day = day
else:
valid_day = 'invalid'
elif month_name in days_30:
if day >= 1 and day <= 30:
valid_day = day
else:
valid_day = 'invalid'
elif month_name in days_28:
if year != 'invalid':
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
if day >= 1 and day <= 29:
valid_day = day
else:
valid_day = 'invalid'
else:
if day >= 1 and day <= 28:
valid_day = day
else:
valid_day = 'invalid'
else:
valid_day = 'invalid'
else:
valid_day = 'invalid'
def valid_month_finding():
global month_name
if month in month_dict.keys():
month_name = month
else:
month_name = 'invalid'
def valid_year_finding():
global year
if year_input > 1900 and year_input <2020:
year = year_input
else:
year = 'invalid'
def birthday_checking():
if valid_day != 'invalid' and month_name != 'invalid' and year != 'invalid':
print 'your birthdate is %d - %s - %d' % (valid_day, month_dict[month_name], year)
else:
print 'Your Birthday is invalid'
valid_year_finding()
valid_month_finding()
valid_day_finding()
birthday_checking()
This code is very inefficient. How can I improve this code?
I know that there are built-in functions to check this, but I am trying to learn coding, which is why I am giving this a try.