Python program Using if elif else statement to Find the Number of Days Present in a Month

Python program Using if elif else statement to Find the Number of Days Present in a Month

In this article, we will be solving question based on Conditional Statements in Python. According to the question we have to find the number of days present in a month using if elif else statement.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python program Using if elif else statement to Find the Number of Days Present in a Month

Python program Using if elif else statement to Find the Number of Days Present in a Month


Logic and Explanation

Question - Write a Python program Using if elif else statement to Find the Number of Days Present in a Month

Python program Using if elif else statement to Find the Number of Days Present in a Month - In this program we have to find the number of days present in month. For this we will be dividing the months in three category one for the months with 31 days, one for the months with 30 days and one for the February.

So, let's start writing with code and the first task will be taking input from the user the name of the month.

month = input("Enter Month")


Thereafter, we will use the "in" operator. Here' you can see in the code below, we created a tuple and then in that tuple we stored the names of the months with 31 days as a string. 

Now, the user enter the name of the month and if that name will be present in this tuple, then we will print that the month has 31 days.

Then, another condition in elif section we create for February which is a special case that has 28 days in a non Leap year and 29 days in a leap year. So this program can be further expanded by asking input from the user for the current year and then printing the number of days in that year. You are advised to try this on your own.

The next and final in the else section, we are only left with the months that have 30 days. So, we print that the month has 30 days.

I hope the logic and the flow of the code will be clear to you. Here's the code for the same.

month = input("Enter Month")
if month.lower() in ("january,march,may,july,august,october, december"):
    print("It has 31 Days")

elif month == "february":
    print("It has 28 in a non Leap year and 29 Days in a Leap year")
else:
    print("It has 30 Days")


Note - Here we used lower() because user can input the name of month with combination of both uppercase and lowercase letters and this can create problem for comparison. So for our convenience, in string comparisons we should convert the inputted string in complete lowercase.

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post