"""DuckieDai Newton's Second Law for TI-84 Plus CE Python."""


def duckiedai_intro(program_name, wait=True):
    """Show the small, reusable DuckieDai program opening."""
    title = program_name[:16]
    empty = 16 - len(title)
    left = empty // 2
    right = empty - left

    print("+----------------------+")
    print("|   " + " " * left + title + " " * right + "   |")
    print("|                      |")
    print("|       __             |")
    print("|   ___(o )>  quack!   |")
    print("|   \\ <_. )           |")
    print("|    `---'             |")
    print("|     by DuckieDai     |")
    print("+----------------------+")
    print("Loading...")

    if wait:
        input("Press enter to start ")
        print("\n" * 6)


def number(prompt):
    """Ask until the user supplies a valid number."""
    while True:
        try:
            value = float(input(prompt))

            if value > -1e100 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter a valid number.")


def positive_number(prompt):
    """Ask until the user supplies a number above zero."""
    while True:
        try:
            value = float(input(prompt))

            if value > 0 and value < 1e100:
                return value

        except ValueError:
            pass

        print("Enter a number above 0.")


def main():
    duckiedai_intro("NEWTON 2ND LAW")

    print("NEWTON'S SECOND LAW")
    print()
    print("Formula: F = m*a")
    print()
    print("1. Solve for Force")
    print("2. Solve for Mass")
    print("3. Solve for Acceleration")
    print()

    choice = input("Choose 1, 2, or 3: ")

    print()

    if choice == "1":
        mass = positive_number("Mass (kg): ")
        acceleration = number("Acceleration (m/s^2): ")

        force = mass * acceleration

        print()
        print("F = m*a")
        print("F = " + str(mass) + " x " +
              str(acceleration))
        print()
        print("Force = " + str(force) + " N")

    elif choice == "2":
        force = number("Net force (N): ")
        acceleration = number("Acceleration (m/s^2): ")

        if acceleration == 0:
            print()
            print("Acceleration cannot")
            print("be zero when solving")
            print("for mass.")
        else:
            mass = force / acceleration

            print()
            print("m = F/a")
            print("m = " + str(force) + " / " +
                  str(acceleration))
            print()
            print("Mass = " + str(mass) + " kg")

    elif choice == "3":
        force = number("Net force (N): ")
        mass = positive_number("Mass (kg): ")

        acceleration = force / mass

        print()
        print("a = F/m")
        print("a = " + str(force) + " / " +
              str(mass))
        print()
        print("Acceleration =")
        print(str(acceleration) + " m/s^2")

    else:
        print("Invalid choice.")

    print()
    print("DuckieDai says: done!")


# TI-Python imports the selected AppVar rather than
# setting __name__ to main.
main()
