I'm teaching myself python. I wrote a fun program that asks your name and date of birth then tells you how old you are in several different formats such as house old you are in days or in months and days or in years, months, and days. I gotta add weeks but that will be easy. I know it's not perfect especially when it comes to calculating days only but it's damn close. In more interested in proper coding techniques. I'm sure the whole thing is probably very sloppy. I also have a strong feeling that I'm not using the def function the way it's intended. Please critique. Show me how you would've wrote it. Thanks.
Age_calc.py:
from datetime import datetime
import calendar
import os
current_day = datetime.now().day
current_month = datetime.now().month
current_year = datetime.now().year
repeat = "Y"
new_info = "Y"
def clear_screen():
# Check if the operating system is Windows
if os.name == 'nt':
os.system('cls') # For Windows
else:
os.system('clear') # For other platforms like macOS and Linux
def days_only(yrs, mos, dys, nm):
dys = dys+(yrs*365)+(mos*30)
print(nm, 'you are', dys, 'days old. Good God!')
def months_days(yrs, mos, dys, nm):
mos = mos+(yrs*12)
print(nm, 'you are', mos, 'months old and', dys, 'days old')
def years_months_days(yrs, mos, dys, nm):
print(nm, 'you are', yrs, 'years,', mos, 'months, and', dys, 'days old!')
clear_screen()
while repeat == "Y":
if new_info == "Y":
name = input("Hello. What is your name? ")
# birth_date = input(f'Okay {name}. Enter your birthday.: (MM/DD/YYYY)')
birth_year = input(f'Hello {name} what year were you born? ')
birth_month = input("What month numerically? (EX: December=12) ")
birth_day = input(f'And what day of the month of {calendar.month_name[int(birth_month)]}? ')
print('\n SEE YOUR EXACT AGE IN:')
print(' 1. DAYS ONLY \n 2. MONTHS AND DAYS ONLY \n 3. YEARS MONTHS AND DAYS \n 4. ALL OF THE ABOVE')
selection = input('\nENTER A NUMBER: ')
clear_screen()
birth_year = int(birth_year)
birth_month = int(birth_month)
birth_day = int(birth_day)
years = current_year - birth_year
months = current_month - birth_month
days = current_day - birth_day
if months < 0:
years = years - 1
months = 12 - abs(months)
if days < 0:
months = months - 1
days = 30 + days
if selection == '1':
days_only(years, months, days, name)
if selection == '2':
months_days(years, months, days, name)
if selection == '3':
years_months_days(years, months, days, name)
if selection == '4':
days_only(years, months, days, name)
months_days(years, months, days, name)
years_months_days(years, months, days, name)
if years < 36:
print("Good for you!")
else:
print("Damn. That's old!")
repeat = input("\nWould you like to try again? (Y/N): ").upper()
if repeat == "Y":
new_info = input('Enter new information? (Y/N):').upper()
clear_screen()
print('Thanks for stopping by', name)
Age_calc.py:
from datetime import datetime
import calendar
import os
current_day = datetime.now().day
current_month = datetime.now().month
current_year = datetime.now().year
repeat = "Y"
new_info = "Y"
def clear_screen():
# Check if the operating system is Windows
if os.name == 'nt':
os.system('cls') # For Windows
else:
os.system('clear') # For other platforms like macOS and Linux
def days_only(yrs, mos, dys, nm):
dys = dys+(yrs*365)+(mos*30)
print(nm, 'you are', dys, 'days old. Good God!')
def months_days(yrs, mos, dys, nm):
mos = mos+(yrs*12)
print(nm, 'you are', mos, 'months old and', dys, 'days old')
def years_months_days(yrs, mos, dys, nm):
print(nm, 'you are', yrs, 'years,', mos, 'months, and', dys, 'days old!')
clear_screen()
while repeat == "Y":
if new_info == "Y":
name = input("Hello. What is your name? ")
# birth_date = input(f'Okay {name}. Enter your birthday.: (MM/DD/YYYY)')
birth_year = input(f'Hello {name} what year were you born? ')
birth_month = input("What month numerically? (EX: December=12) ")
birth_day = input(f'And what day of the month of {calendar.month_name[int(birth_month)]}? ')
print('\n SEE YOUR EXACT AGE IN:')
print(' 1. DAYS ONLY \n 2. MONTHS AND DAYS ONLY \n 3. YEARS MONTHS AND DAYS \n 4. ALL OF THE ABOVE')
selection = input('\nENTER A NUMBER: ')
clear_screen()
birth_year = int(birth_year)
birth_month = int(birth_month)
birth_day = int(birth_day)
years = current_year - birth_year
months = current_month - birth_month
days = current_day - birth_day
if months < 0:
years = years - 1
months = 12 - abs(months)
if days < 0:
months = months - 1
days = 30 + days
if selection == '1':
days_only(years, months, days, name)
if selection == '2':
months_days(years, months, days, name)
if selection == '3':
years_months_days(years, months, days, name)
if selection == '4':
days_only(years, months, days, name)
months_days(years, months, days, name)
years_months_days(years, months, days, name)
if years < 36:
print("Good for you!")
else:
print("Damn. That's old!")
repeat = input("\nWould you like to try again? (Y/N): ").upper()
if repeat == "Y":
new_info = input('Enter new information? (Y/N):').upper()
clear_screen()
print('Thanks for stopping by', name)