smallvt
smallvt

Reputation: 63

Why do I get an error code while importing a function from another file?

main.py sincostan.py

Why am I getting the "No module named "find_sin_cos_tan" " error? I was fairly confident in my program, and got an unexpected error. Any help is much appreciated, I am new to coding and stackoverflow and am struggling to grasp some things. The goal is to create a user-input range of degree values (they input the start and the finish). Then the program is supposed to format all of the data and make a bunch of calculations, among some other things. It should print a horizontal and vertical chart.


def find_sin_cos_tan(start, end):
    degrees = range(start, end+1, 5)
    
    def apply(trig_func):
        return list(map(trig_func, map(math.radians, degrees)))
    sine = [e for e in apply(math.sin)]
    cosine = [e for e in apply(math.cos)]
    tangent = [e for e in apply(math.tan)]

    return sine, cosine, tangent

def get_formatted(start, end, sinlist, coslist, tanlist):
    degrees = ["Degrees:"] + list(map(str, range(start, end+1, 5)))
    sine = ["Sine:"] + ["{:.4f}".format(e) for e in sinlist]
    cosine = ["Cosine:"] + ["{:.4f}".format(e) for e in coslist]
    tangent = ["Tangent:"] + ["{:.4f}".format(e) for e in tanlist]

    return degrees, sine, cosine, tangent

def display_rows(start, end, sinlist, coslist, tanlist):
    print("ROW FORMAT:")
    rows = get_formatted(start, end, sinlist, coslist, tanlist)
    for row in rows:
        row = "".join(e.center(12) for e in row)
        print("-"*len(row))
        print(row)
    print("-"*len(row))
    
def display_columns(start, end, sinlist, coslist, tanlist):
    cols = get_formatted(start, end, sinlist, coslist, tanlist)
    rows = [(d, s, c, t)
            for d, s, c, t in zip(cols[0], cols[1], cols[2], cols[3])]
    for row in rows:
        row = "".join(e.center(12) for e in row)
        print(row)
        
def display_totals(sinlist, coslist, tanlist):
    negative_sine = [e for e in sinlist if e < 0]
    negative_cosine = [e for e in coslist if e < 0]
    ind_tangent = [e for e in tanlist if ((e > 100) or (e < -100))]
    print(f"Number of negative Sine values: {len(negative_sine)}")
    print(f"Number of negative Cosine values: {len(negative_cosine)}")
    print(f"Number of indeterminate Tangent values: {len(ind_tangent)}")

sincostan.py ^

def main():
    import find_sin_cos_tan
    import display_rows
    import display_columns
    import display_totals
    
    
    while True:
        inp = input("Enter 2 numbers between 0 and 360: ")
        if not inp.replace(' ', '0').isdigit():
            print("Input must be two integers")
            break
        start, end = list(map(int, inp.split(" ")))
        if not ((0, 0) <= (start, end) <= (360, 360)):
            print("Invalid input! please try again ( both numbers must be in range [0, 360] )\n")
            continue
        if end < start:
            print(
                "Invalid input! please try again ( second number must be greater than first )\n")
            continue
        if (end - start) > 90:
            print(
                "Invalid input! please try again ( difference should be no more than 90 )\n")
            continue
        print(
            f"Sine, Cosine, Tangent values for integers {start} to {end} in steps of 5")

        sinlist, coslist, tanlist = find_sin_cos_tan(start, end)
        display_rows(start, end, sinlist, coslist, tanlist)
        print()
        display_columns(start, end, sinlist, coslist, tanlist)
        print()
        display_totals(sinlist, coslist, tanlist)
        print()
        
if __name__ == "__main__":
    main()

main.py ^

Upvotes: 0

Views: 45

Answers (1)

Andrew Eckart
Andrew Eckart

Reputation: 1728

find_sin_cos_tan is the name of a function, not a module (file containing Python source code). The function is located inside of a module named sincostan.py, so the correct way to import it is as follows:

def main():
    from sincostan import find_sin_cos_tan
    ...

Upvotes: 1

Related Questions