Please critique my code for fun learning project.

Joined
Jul 20, 2023
Messages
56
Reaction score
2
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)
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
Hello !

as you work on 'console' style code, you can make your code more generics, and preview others uses.


the loop ( the big one ) have to be composed by clean architecture.
you will understand why.

Python:
// c_ is for Custom, coder work



while c_TEXT == "Y":

c_DISPLAY() // print on screen the contents needed, it could change every loop
c_ANSWER() // keyboard input, c_TEXT is set here  by Users
c_USE_ANSWER() // fork to the functions calls, c_ANSWER value will guide through all possibles process available
c_CLEAR() // reset the display, unload unused resources and vars

This little code reduce the contents of the main loop,
and dispatch all 'works' done in several basic functions.

you will need vars :
  • one for the display to print on screen, fullfil by c_USE_ANSWER fork
  • one for the answer to pick from keyboard, set when c_ANSWER


by working on each components here, you will have lot of code for each one.
ideas and solutions will come
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Thanks for the response. That is something I was wondering about. Wether it is better to make a lot of separate functions or to put more code in the main function. If it's better to make a lot of seperate functions, I find it hard to keep track of and share variables considering a variable in a separate function cannot use the same name as the one in the main funct. I do know there is a such thing as a global variable. Just haven't really got that figured that out yet.

Also what do you mean by preview other uses?
 
Last edited:
Joined
Sep 4, 2022
Messages
128
Reaction score
16
it's call function , but they are 'main loop functions' , it cancel a over-crowded main loop, and enhance to ATOMIC code ( code is in lot of small pieces ) , and separate --display / input / process to use--, it's for a --better readability-- too.

when a loop is 200 miles long, you'll get crazy and lost.

by using this among architecture, you have same components as you got, as 'isolated features'.

to switch feedback ( datas ) from each components,
it's simple use few vars, but define them in the 'main scope', before the 'main loop'.
it grants you to fill the vars, and replace the value from 'main loop' to 'components' or from 'components' to 'main loop' at each main loop.

It's not more code, it's clean code
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
Python:
from datetime import datetime
import calendar
import os

message_on_screen = "Hello, choose an option please :/n1 - AGE/n2 - NAME/n3 ADRESS"
age = 0
address = ""
name = ""


def reach_proc():

match c_TEXT:
  case 1:
    message_on_screen = "Age board :"
    age = input("Enter your age please :")
  case 2:
    message_on_screen = "Name board :"
    name = input("Enter your name please")
  case 3:
    message_on_screen = "Address Board :"
    address = input("Enter your Address")
  case _:
    message_on_screen = "See you next time !"



def c_DISPLAY():
    print(message_on_screen)


def c_ANSWER():
    c_TEXT = input("Your Option :")


def c_USE_ANSWER():
    reach_proc()


def c_CLEAR():
    clear_screen()

def clear_screen():

if os.name == 'nt':
os.system('cls') # For Windows
else:
os.system('clear') # For other platforms like macOS and Linux

while c_TEXT == "Y":

c_DISPLAY()
c_ANSWER()
c_USE_ANSWER()
c_CLEAR()



I hope this quick sample code will help you.
if errors they will be easy to fix and find.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top