Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_month(date_string):
- # Split the date string into a list of three strings, representing the day, month, and year
- date_parts = date_string.split("-")
- # Get the month string from the list
- month_string = date_parts[1]
- # Convert the month string to an integer
- month_number = int(month_string)
- # Define a list of month names
- months = [
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December",
- ]
- # Get the month name from the list using the month number
- month_name = months[month_number - 1]
- # Create a dictionary that maps month numbers to month names
- month_dict = {
- 1: "January",
- 2: "February",
- 3: "March",
- 4: "April",
- 5: "May",
- 6: "June",
- 7: "July",
- 8: "August",
- 9: "September",
- 10: "October",
- 11: "November",
- 12: "December",
- }
- # Get the month name from the dictionary using the month number
- month_name = month_dict[month_number]
- # Create a set of all the months in the year
- month_set = {
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December",
- }
- # Create a tuple of the month number and month name
- month_tuple = (month_number, month_name)
- return month_tuple # Note: ganti return variable nya mau return tuples, sets, dicts, or lists?
- # Test the function with the input string '26 - 01 - 2022'
- print(get_month("26 - 01 - 2022"))
Advertisement
Comments
-
- instead of all that unnecessary code just make it shorter with this
- import calendar
- def get_month(date_string):
- # Split the date string into a list of three strings, representing the day, month, and year
- try:
- day, month, year = date_string.split("-")
- # Convert the month string to an integer
- month_number = int(month)
- except (ValueError, TypeError):
- # Handle invalid input
- return None, None, None
- # Get the month name from the calendar module
- month_name = calendar.month_name[month_number]
- return int(day), month_name, int(year)
- # Test the function with the input string '26 - 01 - 2022'
- print(get_month("26-01-2022")) # Output: (26, "January", 2022)
-
- Thank you for your reply. But it is for university assignment. The requirements required the students to use tuples, sets, lists, and dicts data type. 😄
-
- Also, it is the answer I made and pasted the code to pastebin for easy access.
-
- My answer :)
- import calendar
- def get_month(date_string):
- # Split the date string into a list of three strings, representing the day, month, and year
- try:
- day, month, year = date_string.split("-")
- # Convert the month string to an integer
- month_number = int(month)
- except (ValueError, TypeError):
- # Handle invalid input
- return None, None, None
- # Get the month name from the calendar module
- month_name = calendar.month_name[month_number]
- # Create a tuple of the day, month name, and year
- date_tuple = (int(day), month_name, int(year))
- # Create a list of all the months in the year
- months_list = [
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December",
- ]
- # Create a set of all the months in the year
- months_set = {
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December",
- }
- # Create a dictionary that maps month numbers to month names
- month_dict = {
- 1: "January",
- 2: "February",
- 3: "March",
- 4: "April",
- 5: "May",
- 6: "June",
- 7: "July",
- 8: "August",
- 9: "September",
- 10: "October",
- 11: "November",
- 12: "December",
- }
- return date_tuple, months_list, months_set, month_dict
- # Test the function with the input string '26 - 01 - 2022'
- date_tuple, months_list, months_set, month_dict = get_month("26-01-2022")
- print("Date tuple:", date_tuple) # Output: Date tuple: (26, 'January', 2022)
- print("Months list:", months_list) # Output: Months list: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
- print("Months set:", months_set) # Output: Months set: {'March', 'July', 'June', 'January', 'December', 'May', 'August', 'April', 'September', 'February', 'October', 'November'}
- print("Month dict:", month_dict) # Output: Month dict: {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}
- ``
Add Comment
Please, Sign In to add comment
Advertisement