-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_1_coding_project_2315543.py
More file actions
62 lines (48 loc) · 1.95 KB
/
exercise_1_coding_project_2315543.py
File metadata and controls
62 lines (48 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Exercise_1_Coding_project_2315543.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/15rkFuAKhlBDtF4m8DPMuHbvEKDazVJxY
"""
# Import the date library to access today's date
from datetime import date
# Prompt the user to enter their date of birth
user_date_of_birth = input("Please enter your date of birth in this format dd/mm/yyyy: ")
# Split the user's input into individual date parts
date_parts = user_date_of_birth.split("/")
# verify if the user's input follows the specfied date format
if len(date_parts) != 3:
print("Invalid date format. Please enter your date of birth in the format dd/mm/yyyy.")
exit(1)
# Validate the splitted date parts
try:
day = int(date_parts[0])
month = int(date_parts[1])
year = int(date_parts[2])
# Check if the day is within the valid range
if day < 1 or day > 31:
print("Invalid day. Please enter a valid day (1-31).")
exit(1)
# Check if the month is within the valid range
if month < 1 or month > 12:
print("Invalid month. Please enter a valid month (1-12).")
exit(1)
# Check if the year is within the valid range
if year < 0:
print("Invalid year. Please enter a valid year (positive number).")
exit(1)
except ValueError:
print("Invalid date format. Please enter a valid date in the format dd/mm/yyyy.")
exit(1)
# Calculate the user's age
user_date_of_birth = date(year, month, day)
today = date.today()
age = today.year - user_date_of_birth.year
# Check if the user has had a birthday this year
if today.month < user_date_of_birth.month or today.month == user_date_of_birth.month and today.day < user_date_of_birth.day:
age -= 1
# Print the user's age
print("Your are", age, "years old")
# Check if it's the user's birthday today
if today.month == user_date_of_birth.month and today.day == user_date_of_birth.day:
print("Happy birthday!")