Machinist Calculator

I’ve been working on learning Python. Below is a calculator I’m programming with functions that are useful for me. I’ve programmed most of this on my phone, hence all the line breaks for display on a small screen. I keep adding more functions as they occur to me. I will update this page as I go. 

 

import math


def do_over():
    """Creates an error message in case of bad input."""
    print("Error.")
    try_again = input("Do you want to start over? 'Y' for yes. Any other key for no. \n").lower()
    if try_again == "y":
        calculator()
    else:
        exit()


"""This section works with spot drill/countersink info."""
def drill_depth():
    drill_point_angle = float(input("Enter the drill point angle. \n"))
    chamfer_diameter = float(input("Enter the chamfer diameter. \n"))
    hole_diameter = float(input("Enter the hole diameter. \n"))
    depth = round((math.tan(math.radians(180 - drill_point_angle) / 2)) * (chamfer_diameter / 2), 4)
    chamfer_size = round((chamfer_diameter - hole_diameter) / 2, 4)
    print("Countersink to a depth of:", depth)
    print("Chamfer size is:", chamfer_size)


"""This section works with inch size taps."""
def cut_inch():
    nom_diam = float(input("Enter the nominal thread diameter as a decimal. \n"))
    per_engage = float(input("Enter the percent of thread engagement. \n"))
    threads_inch = float(input("Enter the number of threads per inch. \n"))
    tap_drill = round(nom_diam - (per_engage) / (76.98 * threads_inch), 4)
    print("Tap drill size: ", tap_drill)

def form_inch():
    nom_diam = float(input("Enter the nominal thread diameter as a decimal. \n"))
    per_engage = float(input("Enter the percent of thread engagement. \n"))
    threads_inch = float(input("Enter the number of threads per inch. \n"))
    tap_drill = round(nom_diam - (.0068 * per_engage) / threads_inch, 4)
    print("Tap drill size: ", tap_drill)

def which_inch_tap():
    what_tap = int(input("Which type of tap? \n'1' for cut. \n'2' for form. \n"))
    if what_tap == 1:
        cut_inch()
    elif what_tap == 2:
        form_inch()


"""This section works with metric size taps."""
def cut_metric():
    nom_diam = float(input("Enter the nominal thread diameter. \n"))
    per_engage = float(input("Enter the percent of thread engagement. \n"))
    thread_pitch = float(input("Enter the pitch of the thread. \n"))
    tap_drill = round(nom_diam - (per_engage * thread_pitch) / 76.98, 4)
    print("Tap drill size: ", tap_drill)

def form_metric():
    nom_diam = float(input("Enter the nominal thread diameter. \n"))
    per_engage = float(input("Enter the percent of thread engagement. \n"))
    thread_pitch = float(input("Enter the pitch of the thread. \n"))
    tap_drill = round(nom_diam - (per_engage * thread_pitch) / 147.06, 4)
    print("Tap drill size: ", tap_drill)

def which_metric_tap():
    what_tap = int(input("Which type of tap? \n'1' for cut. \n'2' for form. \n"))
    if what_tap == 1:
        cut_metric()
    elif what_tap == 2:
        form_metric()

"""Determines whether inch or metric tap."""
def tap_calculator():
    which_thread = int(input("Which thread? \n'1' for inch(unc/unf). \n'2' for metric. \n"))
    if which_thread == 1:
        which_inch_tap()
    elif which_thread == 2:
        which_metric_tap()
        
        
"""This section works with rotary broaching."""
def rotary_broach_hex():
    hex_flats = float(input("Enter dimension across the flats. \n"))
    drill_size = round(hex_flats * 1.035, 4)
    print("Drill", drill_size)

def rotary_broach_square():
    square_flats = float(input("Enter dimension across the flats. \n"))
    drill_size = round(square_flats * 1.1, 4)
    print("Drill", drill_size)

def rotary_broach_shape():
    what_shape = int(input("Which shape? \n'1' for hex/torx. \n'2' for square. \n"))
    if what_shape == 1:
        rotary_broach_hex()
    elif what_shape == 2:
        rotary_broach_square()


"""This section determines preturn dimensions for hex and square shapes."""
def turn_hex():
    dim_hex = float(input("Enter the dimension across the hex flats as a decimal. \n"))
    turn_dim = round(2 * ((dim_hex / 2) / (math.sin(math.radians(60)))), 4)
    print(f"Turn to {turn_dim}.")

def turn_square():
    dim_square = float(input("Enter the dimension across the square flats as a decimal. \n"))
    turn_dim = round(2 * ((dim_square / 2) / (math.sin(math.radians(45)))), 4)
    print(f"Turn to {turn_dim}.")

def turn_random_shape():
    num_sides = int(input("Enter the number of sides. \n"))
    center_dist = float(input("Enter the distance from the center of the shape to the center of a flat edge. \n"))
    turn_dim = round((2 * (center_dist)) / (math.sin(math.radians((180 - (360 / num_sides)) / 2))), 4)
    print(f"Turn to {turn_dim}.")

def preturn_shape():
    turn_shape = int(input("Preturn which shape? \n'1' for hex. \n'2' for square. \n'3' for random shapes. \n"))
    if turn_shape == 1:
        turn_hex()
    elif turn_shape == 2:
        turn_square()
    elif turn_shape == 3:
        turn_random_shape()
    else:
        do_over()


"""This section works with speeds and feeds."""
def spindle_speed():
    surface_footage = float(input("What is the recommended surface footage? \n"))
    diameter_size = float(input("What is the diameter? \n"))
    revs_per_minute = round((12 * surface_footage) / (math.pi * diameter_size), 0)
    print("RPM is:", revs_per_minute)

def feed_rate():
    num_of_teeth = int(input("Enter the number of cutting teeth. \n"))
    feed_per_tooth = float(input("Enter the recommended feed per tooth. \n"))
    revs_per_minute = float(input("Enter the RPM. \n"))
    feed_rate = round(revs_per_minute * num_of_teeth * feed_per_tooth, 2)
    print(f"Feed rate is: {feed_rate} IPM.")

def speed_or_feed():
    speed_feed = int(input("Do you need speed or feed? \n'1' for speed. \n'2' for feed. \n"))
    if speed_feed == 1:
        spindle_speed()
    elif speed_feed == 2:
        feed_rate()


def weight_matl():
    """Determines the weight of materials based on size and shape."""
    sel_matl = int(input(
        "Select material. \n'1' for steel/stainless. \n'2' for aluminum/granite. \n'3' for titanium. \n'4' for brass/bronze/inconel. \n"))
    if sel_matl == 1:
        """steel/stainless"""
        weight_matl = .2833
    elif sel_matl == 2:
        """aluminum/granite"""
        weight_matl = .0975
    elif sel_matl == 3:
        """titanium"""
        weight_matl = .163
    elif sel_matl == 4:
        """brass/bronze/inconel"""
        weight_matl = .308
    else:
        do_over()
    sel_shape = int(input("Select shape. \n'1' for rectangular bar. \n'2' for round rod. \n"))
    if sel_shape == 1:
        len_bar = float(input("Length in inches. \n"))
        wth_bar = float(input("Width in inches. \n"))
        hgt_bar = float(input("Height in inches. \n"))
        weight_bar = round(((len_bar * wth_bar * hgt_bar) * weight_matl), 2)
        print(f"Weight is: {weight_bar} lbs.")
    elif sel_shape == 2:
        diam_rod = float(input("Enter the diameter in inches. \n"))
        len_rod = float(input("Length in inches. \n"))
        weight_rod = round(((diam_rod / 2) ** 2) * (math.pi) * (len_rod) * weight_matl, 2)
        print(f"Weight is {weight_rod} lbs.")
    else:
        do_over()


def dec_hours():
    """Converts decimal time to hours:minutes."""
    dec_hrs = float(input("Enter time as a decimal. \n"))
    hours = int(dec_hrs)
    minutes = (dec_hrs * 60) % 60
    print("%d:%02d" % (hours, minutes))





def do_what():
    """This makes it work. Call new functionality here."""
    do_what = int(input(
        "What do you want to do? \n'1' for spot drill/countersink depth. \n'2' for tap drill info. \n'3' for rotary broach info. \n'4' for preturn dimensions for hex and square shapes. \n'5' for material weights. \n'6' to convert decimal time to hours:minutes. \n'7' for speeds and feeds. \n"))
    if do_what == 1:
        drill_depth()
    elif do_what == 2:
        tap_calculator()
    elif do_what == 3:
        rotary_broach_shape()
    elif do_what == 4:
        preturn_shape()
    elif do_what == 5:
        weight_matl()
    elif do_what == 6:
        dec_hours()
    elif do_what == 7:
        speed_or_feed()


def calculator():
    stop_going = False
    while not stop_going:
        do_what()
        carry_on = input("Do you want to continue? Y or N. \n").lower()
        if carry_on == "y":
            stop_going = False
        elif carry_on == "n":
            stop_going = True
        else:
            print("Error")
            try_again = input("Do you want to continue? 'Y' for yes, any other key for no. \n").lower()
            if try_again == "y":
                stop_going = False
            else:
                stop_going = True


calculator()