Write a program that asks the user for a number in the range 1 through 12. The program should display the corresponding month of the year, where 1 = January, 2 = February, 3 = March, 4 = April, 5 = May, 6 = June, 7 = July, 8 = August, 9 = September, 10 = October, 11 = November, and 12 = December. The program should display an error message if the user enters a number that is outside the range of 1 through 12
The program is implemented in python 3.
Python3 code:
# Take input
n = int(input("Enter a number in the range 1 to 12: "))
# Print the output using if-else conditions
if n == 1:
print("The month is January")
elif n == 2:
print("The month is February")
elif n == 3:
print("The month is March")
elif n == 4:
print("The month is April")
elif n == 5:
print("The month is May")
elif n == 6:
print("The month is June")
elif n == 7:
print("The month is July")
elif n == 8:
print("The month is August")
elif n == 9:
print("The month is September")
elif n == 10:
print("The month is October")
elif n == 11:
print("The month is November")
elif n == 12:
print("The month is December")
else:
print("You have entered a number that is outside the range of 1 through 12")
Please refer to the following picture for the source code and sample execution:
Get Answers For Free
Most questions answered within 1 hours.