Python Program to display the month name based on user input

 

Python Program to display the month name based on user input using methods

Write a Python program that has a method called print_month_name() which accepts a number in the range of 1 to 12 and prints the month name.

For example, if the user enters month = 1, then display the month name as January.

Video Tutorial:

Steps:

1. First we, define the print_month_name() method, that accepts an argument say x.

2. Next, we check whether the value of x 1, then print January, if x is 2, then print February, and so on.

3. Next, read the year from the user and pass the year to the is_leap_year() method.

Source code of Python Program to display the month name based on user input

def print_month_name(x):
     if (x==1):
         print ("Jan")
     if (x==2):
         print("Feb")
     if (x==3):
         print("March")
     if (x==4):
         print("April")
     if (x==5):
         print("May")
     if (x==6):
         print("June")
     if (x==7):
         print("July")
     if (x==8):
         print("August")
     if(x==9):
         print("September")
     if(x==10):
         print("October")
     if(x==11):
         print("November")
     if(x==12):
         print("December")
     if(x<1 or x>12):
         print("Invalid input")
 month = int(input("Enter the month number: "))
 print_month_name(month)

Output:

Case 1:

Enter the month number: 1
Jan

Case 2:

Enter the month number: 6
June

Case 3:

Enter the month number: 13
Invalid input

Case 4:

Enter the month number: -10
Invalid input

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *