PYTHON – NameError when calling return value from function (out of order?)

  Kiến thức lập trình

Per the code below, I’m getting this error:

NameError: name ‘leap_status’ is not defined

The def days_in_month() is looking for the status of leap_status which should be defined already but, for some reason, it seems to be calling it before it’s defined by the is_leap() function?

I feel like I missed something about how the return value is stored or something..

def is_leap(year):
  if year % 4 == 0:
    if year % 100 == 0:
      if year % 400 == 0:
        leap_status = True
      else:
        leap_status = False
    else:
      leap_status = True
  else:
    leap_status = False
  return leap_status

def days_in_month(year, month):
    if leap_status == True:
        month_days = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    else:
      month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 
    num_days_in_month = month_days[month -1]
    return num_days_in_month


#Year Input from user
year = int(input()) # Enter a year
#Function to calculate if Leap Year
is_leap(year)
#Month Input from User
month = int(input())
#Function to determine how many days in month
#depending on if Leap Year is True
days_in_month(year, month)
days = days_in_month(year, month)

New contributor

J W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT