Given the year and the month as input to the python program, The program should evaluate the number of days in the particular month and print the number of these as an output to the user.
The leap year also should be taken care of as part of logic.
Write a Python Program to Display The Number of Days in a Given Month
Next take two variables, yeah and month and ask a user to enter the year and month using input function call and then convert it into an integer.
Once we get the year and month as an integer variable, take another II variable month_list, month list will contain the two sub lists, that is [2] for February and another sub list [4, 6, 9, 11] for April, June , September and November. These are the months that have 30 days in the calendar
Let's only take the months that are are having 28 and 30 days into a month list
year = int(input("Enter a year : "))
month = int(input("Enter a month : "))
month_list = [[2], [4, 6, 9, 11]]
Take a statement and check if the month is valid or not using a comparison operator. now inside the if statement take another if statement and check if month is February. if so check for the leap year and print the number of days has 29 else-if non leap year print it as 28
if 1 <= month <= 12:
if month in month_list[0]:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("Number of days is : ", 29)
else:
print("Number of days is : ", 28)
elif month in month_list[1]:
print("Number of days is : ", 30)
else:
print("Number of days is : ", 31)
else:
print("Month is Invalid !")
Use the else part and check if month is part of 30 day month list as shown above if so print the number of days are 30 else print the number of days are 31.
final program to print the number of days in particular month and year
Write a Python Program to Find Number of Days in a Given Month of a Given Year
=================================================================
year = int(input("Enter a year : "))
month = int(input("Enter a month : "))
month_list = [[2], [4, 6, 9, 11]]
if 1 <= month <= 12:
if month in month_list[0]:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("Number of days is : ", 29)
else:
print("Number of days is : ", 28)
elif month in month_list[1]:
print("Number of days is : ", 30)
else:
print("Number of days is : ", 31)
else:
print("Month is Invalid !")
Output for the leap year
============================
Enter a year : 2024
Enter a month : 2
Number of days is : 29
Output for the non leap year
==============================
Enter a year : 2022
Enter a month : 2
Number of days is : 28
Output for month having 30 days
=================================
Enter a year : 2022
Enter a month : 4
Number of days is : 30
Output month having 31 days
===============================
Enter a year : 2022
Enter a month : 1
Number of days is : 31
Extra Reading
Let's try to solve another variant of the same program, that is, user has been given only a month value and you have to print the number of days present in the month. the program goes something like this…
Write a Program to Read Any Month Number in Integer And Display The Number of Days For This Month
To solve this you have to remember only the below nested list.
month_list = [[2], [4, 6, 9, 11]]
The first sub list [2] represents the February month
The next sub list [4, 6, 9, 11] Represent the month number that contains 30 days
To Remember [4, 6, 9, 11] these values 4 and 6 are even numbers (rainy season) 9 and 11 are odd numbers (cyclonic season).
month = int(input("Enter a Month : "))
month_list = [[2], [4, 6, 9, 11]]
if 1 <= month <= 12:
if month in month_list[0]:
print("Number of days in this month is : 28/ 29 days")
elif month in month_list[1]:
print("Number of days in this month is : 30 days")
else:
print("Number of days in this month is : 31 days")
else:
print("Invalid Month !!")
Number of days in this month is : 30 days
Invalid Month !!
Number of days in this month is : 28/ 29 days
Conclusion
==================
Try to optimize the above program according to your requirements. comment down below if you have any suggestions or queries
0 Comments