Python program to print the number of days in a month using switch statements

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to print the number of days in a month using switch statements.

To print the number of days in a month using a switch statement in Python, you can use the following code:

Python program to print the number of days in a month using switch statements

def days_in_month(month):
  month = month.lower()
  switch = {
    "january": 31,
    "february": 28,
    "march": 31,
    "april": 30,
    "may": 31,
    "june": 30,
    "july": 31,
    "august": 31,
    "september": 30,
    "october": 31,
    "november": 30,
    "december": 31
  }
  return switch.get(month, "Invalid month")

month = "February"
print(days_in_month(month))

Output

28

Explanation

This code defines a function that takes a month as input and returns the number of days in that month. The input month is first converted to lowercase to avoid case sensitivity.

The switch statement is implemented using a dictionary, with the keys representing the months and the values representing the number of days in each month.

The get method is used to retrieve the value for the specified key (i.e., month). If the month is not found in the dictionary, the function returns the string “Invalid month”.

In the example above, the input month is “February”, which has 28 days, so the output would be 28.

Program to find the largest among 2 numbers, 3 numbers using Python

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment