Month Name Handling¶
Continuing on from the previous exercise Modify Month Name, implement a validation loop with try/except
to handle the error raised by your modified month_name
function.
What you should do¶
Create a
main
function to ask the user for a month number.Attempt to pass the choice to the
month_name
function even if it is an invalid month number.Take note of the error that it causes when given an invalid month number and set up a
try/except
structure to handle that particular error. In the except, simply state that the option they entered was invalid.Use a loop to force the user to enter a valid number. It should not quit until they enter one.
Enter a month number: 55 Invalid number. Pick 1-12 Enter a month number: 13 Invalid number. Pick 1-12 Enter a month number: 0 Invalid number. Pick 1-12 ...
Include in your
try/except
the ability to handle the error that occurs when the user enters a string value instead of a validint
.
©2021 Daniel Gallo
This work is licensed under Attribution-NonCommercial-ShareAlike 4.0 International