AyanUpadhaya

Age CalCulator Python Project

May 20th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. from datetime import datetime
  2.  
  3. current=datetime.date(datetime.now())
  4.  
  5. data=str(current).split('-')
  6.  
  7. print(f"Current Date:{current}")
  8.  
  9. current_year=int(data[0])
  10. current_month=int(data[1])
  11. current_day=int(data[2])
  12.  
  13.  
  14. thirty_first_days_month=[1,3,5,7,8,10,12]
  15. thirty_days_month=[9,4,6,11]
  16. twenty_days_month=[2]
  17.  
  18.  
  19. def ageCalCulator(year,month,day):
  20.     #if year less than curren_year and month equal to current month and day equal to current day
  21.     if (year<current_year and month==current_month and day==current_day and month<=12):
  22.         total_year=current_year-year
  23.         total_month=(current_month+12)-(month+1)
  24.         total_day=current_day-day  
  25.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  26.  
  27.     #if month is equal to current month but day is less than current day
  28.     if (year<current_year and month==current_month and day<current_day and month<=12):
  29.         total_year=current_year-year
  30.         total_month=(current_month+12)-(month+1)
  31.         total_day=current_day-day  
  32.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  33.  
  34.     #if year less than current year and month less than current month and day less than current day
  35.     if (year<current_year and month<current_month and day<current_day and month<=12):
  36.         total_year=current_year-year
  37.         total_month=current_month-month
  38.         total_day=current_day-day
  39.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  40.    
  41.     #if year less than current year and month less than current month and day greater than current day
  42.     if (year<current_year and month<current_month and day>current_day and month<=12):
  43.         total_year=current_year-year
  44.         total_month=(current_month-1)-month
  45.        
  46.         if month in thirty_days_month:
  47.             total_day=(current_day+30)-day
  48.         if month in thirty_first_days_month:
  49.             total_day=(current_day+31)-day
  50.         if month in twenty_days_month:
  51.             total_day=(current_day+30)-day
  52.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  53.  
  54.     #if year less than current year and month less than current month and day equal to current day
  55.     if (year<current_year and month<current_month and day==current_day and month<=12):
  56.         total_year=current_year-year
  57.         total_month=(current_month-1)-month
  58.         total_day=current_day-day
  59.        
  60.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  61.    
  62.     #if both month and day greater than current day
  63.     if (year<current_year and month>current_month and day>current_day and month<=12):
  64.         total_year=(current_year-1)-year
  65.         total_month=(current_month+12)-(month+1)
  66.  
  67.         if month in thirty_days_month:
  68.             total_day=(current_day+30)-day
  69.         if month in thirty_first_days_month:
  70.             total_day=(current_day+31)-day
  71.         if month in twenty_days_month:
  72.             total_day=(current_day+30)-day
  73.        
  74.         print(f"you are {total_year} years {total_month} months and {total_day} days old")
  75.    
  76.     if month>12 or day >31:
  77.         print("Wrong Data input")          
  78.  
  79. def main():
  80.     try:
  81.         birth_year=int(input("Enter Birth Year:"))
  82.         birth_month=int(input("Enter Birth Month:"))
  83.         birth_day=int(input("Enter Birth Day:"))
  84.         ageCalCulator(birth_year,birth_month,birth_day)
  85.     except ValueError:
  86.         print("Wrong input start again?(y/n)")
  87.         op=input()
  88.         if op.lower()=='y':
  89.             main()
  90.         else:
  91.             exit()
  92.  
  93.  
  94. if __name__=="__main__":
  95.     main()
Add Comment
Please, Sign In to add comment