75.3k views
3 votes
Write a function called is_valid_month which takes a string parameter. The function should return true if the string is the valid name of a calendar month and false otherwise. The function should allow for both upper and lower case characters. Which of the following options correctly represents the function definition?

1) def is_valid_month(month: str) → bool:
2) def is_valid_month(month: String) → boolean:
3) def is_valid_month(month: string) → boolean:
4) def is_valid_month(month: String) → bool:

User Ltuska
by
7.8k points

1 Answer

5 votes

Final answer:

The correct definition for a function that checks for a valid calendar month is option 1, which uses Python syntax with the correct types 'str' for the input and 'bool' for the return value.

Step-by-step explanation:

The correct option for defining a function is_valid_month that checks if a string represents a valid calendar month is Option 1:

def is_valid_month(month: str) -> bool:

This is the correct syntax for type hinting in Python. The function signature indicates that the argument month is expected to be a string (str), and the function will return a boolean value (bool). Python's type hints don't recognize String or boolean as valid types, hence options 2, 3, and 4 are incorrect.

An example implementation of the function could be:

def is_valid_month(month: str) -> bool:
valid_months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
return month.capitalize() in valid_months

This implementation creates a list of valid month names, capitalizes the input to handle different cases, and then checks if the provided month is in the list of valid months.

Therefore, the correct option for defining a function is_valid_month that checks if a string represents a valid calendar month is Option 1.

User Mtn Pete
by
7.4k points