Computer Science, asked by Arshaquemirash6049, 1 year ago

Write a program in python to find the number of days in a month thereby prompting the user to enter the month (for example if the entered month is 2, i.e. february then ask user to enter the year and check whether the year is a leap year or not (a leap year is divisible by 4 but not by 100 or divisible by 400) and display number of days accordingly and if the entered month is from the list (1,3,5,7,8,10,12) then display the number of days equal to 31 and if the list is from (4,6,9,11) then display the number of days equal to 30 as "there are n number of days in the month m" otherwise display the message "not a valid month".

Answers

Answered by BhavyaRajput1
8
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
Similar questions