9.1 - DOB
Problem
Write a procedure that will accept a date of birth (DOB) string in the format “dd/mm/yyyy”.
The day, month and year should then be displayed seperately.
Example run
Enter DOB (dd/mm/yyyy): 02/09/1990 Day: 02 Month: 09 Year: 1990
| 1 | Split date | IN | date |
| OUT |
1.1 Display “Day” and date[0:2]
1.2 Display “Month” and date[3:5]
1.3 Display “Year” and date[6:10]
# Main
date = input("Enter DOB (dd/mm/yyyy): ")
split_date(date)
def split_date(date):
print("Day", date[0:2])
print("Month", date[3:5])
print("Year", date[6:10])
# Main
date = input("Enter DOB (dd/mm/yyyy): ")
split_date(date)
"""
Procedure: split_date(date)
---------------------------
This procedure takes a date string in the format dd/mm/yyyy as a parameter.
It uses substrings to extract:
- the day (characters 0–1),
- the month (characters 3–4),
- the year (characters 6–9).
It then prints each part separately.
"""
def split_date(date):
print("Day", date[0:2])
print("Month", date[3:5])
print("Year", date[6:10])
# --- Main program ---
# Ask the user for their date of birth in dd/mm/yyyy format
date = input("Enter DOB (dd/mm/yyyy): ")
# Call the procedure with the user’s date as the parameter
split_date(date)
Extension
Update the procedure so that it will ensure a valid date has been entered before it displays the seperate day, month and year.
- Day: 1-31
- Month:1-12
- Year: 1900 – current
Enter DOB (dd/mm/yyyy): 02/13/1990 Invalid Enter DOB (dd/mm/yyyy): 02/09/1990 Day: 02 Month: 09 Year: 1990