Write a python program that reads an integer representing a month of the year, and then print the name of the month. Your program must include validation and error handling code. Thus if the user enters a value outside the range of 1-12 or characters that cannot be converted to an integer, your code must print the appropriate message and continue without crashing.
Hint: you may not use any 'if' statements
month = {
1:'January',
2:'February',
3:'March',
4:'April',
5:'May',
6:'June',
7:'July',
8:'August',
9:'September',
10:'October',
11:'November',
12:'December'}
try:
n = int(input('enter the month number: '))
print(month[n])
except (ValueError, KeyError):
print('incorrect input enter integer number from 1 to 12')
Comments
Leave a comment