Answer to Question #214138 in Python for Lia

Question #214138

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


1
Expert's answer
2021-07-06T03:12:36-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS