Reputation: 748
I've been trying to work out a way to accomplish what's in the title, without using any imported calendar/datetime libs from Python. There's little function at the top for checking whether or not the year is a leap year, which I want to be able to reference when printing the number of days in a given February, however I'm not too sure how to do so. (I've guessed with something like output. bla bla)
So far I've come up with something like this, which should make clear what I want to do, but I'm still a bit new to Python, so I would love a few tips/help on fixing up my code for the task.
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month == 'September' or month == 'April' or month == 'June' or month == 'November'
print 30
elseif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October'\
or month== 'December'
print 31
elseif month == 'February' and output.is_leap_year = True
print 29
elseif month == 'February' and output.is_leap_year = False
print 28
else print 'Blank'
Ok I've fixed up my code, and it seems to output the correct data for every month but February:
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month in ['September', 'April', 'June', 'November']:
print 30
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print 31
elif month == 'February' and is_leap_year == True:
print 29
elif month == 'February' and is_leap_year == False:
print 28
Any hints to fix up outputting for February?
EDIT: Just needed to add the argument year when referencing the first function. Here is the 100% working code for future reference:
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year(year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month in ['September', 'April', 'June', 'November']:
print 30
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print 31
elif month == 'February' and is_leap_year(year) == True:
print 29
elif month == 'February' and is_leap_year(year) == False:
print 28
else:
return None
Upvotes: 3
Views: 46016
Reputation: 1
*# Write a function that determines how many days there are in a particular month.
def year():
a = int(input("insert your year"))
def month():
z = int(input("enter month 1-12 "))
if z==9 or z==4 or z ==6 or z==11:
return print(30)
elif z==1 or z==3 or z==5 or z==7 or z==10 or z==12:
return print(31)
elif z ==2 and a % 4==0:
return print("29 its a leap year")
elif z ==2 and a % 4==1:
return print(28)
month()
year()
Upvotes: 0
Reputation: 1
month=input("month")
year=int(input("year"))
if year%4==0:
year=('leap year')
if month in ['September', 'April', 'June', 'November']:
print ("30")
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print ("31")
elif month == 'February' and year == "leap year":
print ("29")
elif month == 'February' and year != "leap year":
print ("28")
else:
print("none")
Upvotes: 0
Reputation: 1
month = int (input ('month (1-12): '))
if month < 13:
if month == 2:
year = int (input ('year: '))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print ('29')
else:
print ('28')
else:
print ('29')
else:
print ('28')
elif month >= 8:
if month % 2 == 0:
print ('31')
else:
print ('30')
elif month % 2 == 0:
print ('30')
else:
print ('31')
else:
print ('Only 1-12 accepted')
Upvotes: 0
Reputation: 1
"""
Takes the year and month as input and returns the no. of days
"""
def is_leap_year (year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
def days_in_month(month, year):
if month == 'September' or month == 'April' or month == 'June' or month == 'November':
result=30
elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October'or month== 'December':
result=31
elif month == 'February' and output.is_leap_year ==True:
result=29
elif month == 'February' and output.is_leap_year == False:
result=28
return result
print(is_leap_year(2016))
print(days_in_month('September',2016))
Upvotes: 0
Reputation: 913
Some syntax error in your code:
def days_in_month(month,year)
. Python use indentation to separate code blocks. This is the error you given in comment.elseif
in python, it should be elif
output.is_leap_year = True
, it should be is_leap_year(year) == True
. The False
part should be changed too.after if
statement and else
there should be a :
, like
if month == 'September' or month == 'April' or month == 'June' or month == 'November':
print 30
elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October' or month== 'December':
print 31
elif month == 'February' and is_leap_year(year) == True:
print 29
elif month == 'February' and is_leap_year(year) == False:
print 28
else:
print 'Blank'
Upvotes: 2
Reputation: 2753
A more pythonic approach would be to define the mapping in a dictionary, then simply retrieve the values from the dictionary.
Try it out:
days_in_month_dict = {"January": 31, "February": 28,
"March": 31, "April": 30,
"May": 31, "June": 30,
"July": 31, "August": 31,
"September": 30, "October": 31,
"November": 30, "December": 31}
def is_leap_year(year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
def days_in_month(year, month):
if is_leap_year(year) and month == "February":
return 28
try:
#attempt to get value from dictionary
return days_in_month_dict[month]
except KeyError:
#key does not exist, so we caught the error
return None
Upvotes: 4