Answer to Question #214821 in Python for LAYLA

Question #214821
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.)
Examples:
# Input: -5 Output: Please enter a number between 1 and 12:
# Input: gkl Output: Please enter numbers only
# Input: 6 Output: June [40]
1
Expert's answer
2021-07-07T18:15:52-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):
    print('Please enter numbers only.')
except(KeyError):
    print('Please enter integer between 1 and 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