Error in checking validity of month in python function

Viewed 97
month_names=["january","february","march","april",
"may","june","july","august","september","october","november","december"]
day_in_month=[31,28,31,30,31,30,31,31,30,31,30,31]
date =" "
a,b=date.split(" ")


b=int()
x=False
def is_a_valid_date(date):
  global x
  for i in range(len(month_names)):
     if a==month_names[i]:
       if b<=day_in_month[i]:
           x=True
           print("h")
  return x     

 
print(is_a_valid_date("february 21"))

ValueError: not enough values to unpack(expected 2,got 1)
Trying to check if the given string in input is a valid month mentioned in month_names And the date followed by it is valid too if compared with days in day_in_month

3 Answers

You should avoid using global and you not use your parameter date at all!

month_names = [
    "january","february","march","april",
    "may","june","july","august","september","october","november","december"
             ]
day_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]


def is_a_valid_date(date):
  month, day = date.split(" ")  # split with space the month and day
  month_index = month_names.index(month)  # find the index of the month_index from the month_names
  day_as_int = int(day)  # convert the day to int for compering
  return day_as_int <= day_in_month[month_index] # check if it bigger

 
print(is_a_valid_date("february 29")) # False
print(is_a_valid_date("february 28")) # True

If you want to catch the edge cases like upper case and worng input:

month_names = [
    "january","february","march","april",
    "may","june","july","august","september","october","november","december"
             ]
day_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]


# make it lower case
def is_a_valid_date(date):
    try:
        month, day = date.lower().split(" ")  # split with space the month and day
        month_index = month_names.index(month)  # find the index of the month_index from the month_names
        day_as_int = int(day)  # convert the day to int for compering
        return day_as_int > 0 and day_as_int <= day_in_month[month_index] # check if it bigger
    except ValueError:
        return False
 
print(is_a_valid_date("february 29")) # False
print(is_a_valid_date("february 28")) # True
print(is_a_valid_date("febGuary 28")) # False
print(is_a_valid_date("february 0")) # False
print(is_a_valid_date("February 1")) # True
print(is_a_valid_date("February1")) # False
print(is_a_valid_date("February gasf")) # False
print(is_a_valid_date("")) # False

Try this:



month_names=["january","february","march","april",
"may","june","july","august","september","october","november","december"]
day_in_month=[31,28,31,30,31,30,31,31,30,31,30,31]

x=False
def is_a_valid_date(date):
  global x
  a,b=date.split(" ")
  for i in range(len(month_names)):
     if a==month_names[i]:
       if int(b)<=day_in_month[i]:
           x=True
           print("h")
  return x     

 
print(is_a_valid_date("february 21"))

Output:

h
True

If you have the year and month, you can use calendar.monthrange(int year, int month) to get the number of days in the month

https://docs.python.org/3/library/calendar.html#calendar.monthrange

import calendar
_, days_in_month = calendar.monthrange(2021, 1)
# days_in_month = 31

You should refactor to use this as it will also factor in leap year correctly without extra conditions/data-sets

Related